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

标签 javascript html ajax

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

相关文章:

javascript - Offline.js - 在线时检查?

html - 如何使用 Bootstrap 制作水平导航栏?

html - 四列对两列的响应

javascript - 使用 formData 的 Ajax 文件上传在大文件上失败

jquery - 在 jQuery 中获取多个文本框值 - MVC

javascript - 使用 UI 反馈在多个元素上切换类

javascript - MVC函数没有定义在onclick处?

php - 通过 Ajax 按用户名获取 WordPress 用户电子邮件

javascript - javascript中声明变量时,默认值是null吗?

html - 垂直、右对齐和底部对齐;跨浏览器