简体   繁体   English

ajax:本地服务器无数据

[英]ajax: no data from local server

I'm using php on a linux machine. 我在Linux机器上使用php。 My html code issues an ajax request to the local apache server ( http://localhost ), and the data from the server should be printed out on the screen. 我的html代码向本地apache服务器( http:// localhost )发出ajax请求,并且来自服务器的数据应在屏幕上打印出来。 However, nothing gets printed. 但是,什么都不会打印。

The code on the "client" side (the html file which I load in the browser) is: “客户端”端的代码(我在浏览器中加载的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>

and the "server" script (which is /var/www/s.php) is: 并且“服务器”脚本(/var/www/s.php)为:

<html>
    <body>
        <?php
            echo date("H:i:s"); 
        ?>
    </body>
</html>

Any suggestions? 有什么建议么?

TIA TIA

You should debug your code 您应该调试代码

  1. Check Apache access log that s.php was loaded 检查Apache访问日志是否已加载s.php
  2. If it loaded then add debug alert function to onreadystatechange callback function 如果已加载,则将调试警报功能添加到onreadystatechange回调函数
  3. If this function is called then check what return code it received: alert(ajaxRequest.readyState); 如果调用了此函数,则检查它收到了什么返回代码: alert(ajaxRequest.readyState);
  4. If code is 4 then check what content it returned: alert(ajaxRequest.responseText); 如果代码为4,则检查返回的内容: alert(ajaxRequest.responseText);

页面加载时似乎没有任何调用ajaxFunction的内容,因此从不发送请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM