gpt4 book ai didi

javascript - ajax:本地服务器没有数据

转载 作者:行者123 更新时间:2023-11-28 09:27:25 25 4
gpt4 key购买 nike

我在 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

最佳答案

你应该调试你的代码

  1. 检查 Apache 访问日志是否已加载 s.php
  2. 如果已加载,则将调试警报功能添加到 onreadystatechange 回调函数
  3. 如果调用此函数,则检查它收到的返回代码:alert(ajaxRequest.readyState);
  4. 如果代码为 4,则检查它返回的内容:alert(ajaxRequest.responseText);

关于javascript - ajax:本地服务器没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14173981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com