好的,问题就在这里。我有以下 PHP 代码。
    <?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysqli_close($con);
?>
我从W3Schools上下来,在数据库所在的服务器上工作正常。但是,我想做的是使用以下 Javascript 在另一个站点上显示它。
    function displaydata()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("datalist").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","phpscripthere.com/myphp.php",true);
xmlhttp.send();
}
上面的 javascript 有效,但仅当它位于数据库所在的服务器上时。我需要的是让 Javascript 在另一个位置工作,而 PHP 仍然可以存储在主服务器上。