我在 Linux 机器上使用 php。我的 html 代码向本地 apache 服务器( http://localhost )发出 ajax 请求,来自服务器的数据应该打印在屏幕上。但是,没有打印任何内容。
“客户端”端的代码(我在浏览器中加载的html文件)是:
<html>
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if( ajaxRequest.readyState == 4 ){
document.writeln( ajaxRequest.responseText );
}
}
ajaxRequest.open("GET", "http://localhost/s.php", true);
ajaxRequest.send(null);
}
</script>
</body>
</html>
“服务器”脚本(/var/www/s.php)是:
<html>
<body>
<?php
echo date("H:i:s");
?>
</body>
</html>
有什么建议吗?
TIA
最佳答案
你应该调试你的代码
- 检查 Apache 访问日志是否已加载 s.php
- 如果已加载,则将调试警报功能添加到 onreadystatechange 回调函数
- 如果调用此函数,则检查它收到的返回代码:
alert(ajaxRequest.readyState);
- 如果代码为 4,则检查它返回的内容:
alert(ajaxRequest.responseText);
关于javascript - ajax:本地服务器没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14173981/