2016-07-24 115 views
0

自从大学以来,我从未真正做过很多web开发,但现在我需要组建一个小型网站来显示一些数据库数据。我认为AJAX将是解决这个问题的最佳方式。AJAX响应0

所以我尝试了一个非常简单的任务,只是为了让我的头绕过XMLHttpRequest对象。

我做了如下的HTML页面:

<!DOCTYPE html> 
<html> 
<script> 
    function loadDoc() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    document.getElementById("response").innerHTML = xhttp.status; 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
    document.getElementById("target").innerHTML = xhttp.responseText; 
    } 
    }; 

    xhttp.open("GET", "http://www.********.***/phpTest/findCalls.php", true); 

    xhttp.send(); 
} 

</script> 

    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>New Web Project</title> 
    </head> 
    <body>   
     <button type="button" onclick="loadDoc()">Click Me</button> 

     <p id="target">This is where the text goes.</p> 
     <p id="response">This is the response.</p> 
    </body> 
</html> 

然后我做了一个PHP页面,并把它的服务器上,这是一个简单的页面,与一行文本的答复。它看起来像这样:

<?php 

    echo "From PHP."; 

?> 

如果我通过Web浏览器访问PHP页面,它显示了一个页面'From PHP。'。然而,当试图运行html页面(通过Aptana Studio)时,它经常失败,xhttp.status返回0.

我试过关闭防火墙,这没有帮助。

我试着将请求的目标更改为本地存储在我的机器上的文本文件,并且这个工作正常,但是我没有安装本地PHP服务器来测试它。

我希望Stack Overflow上的某个人能够看到我忽略的一些东西。

预先感谢您。

+0

+0

如果您在一段时间内还没有完成网络开发,那么您最好在标准PHP脚本中返回动态数据,而不是通过将AJAX添加到等式来使事情复杂化。 –

+0

它会工作,只是把一个退出在PHP后echo –

回答

-1

相反的JavaScript,你可以使用jQuery Ajax调用:

$.ajax({ 
    url : "http://pardipphp.com/data", 
    type : "GET", 
    data : { "firstName": "Pardip", "lastName": "Bhatti" }, 
    beforeSend: function() { 
     // code here 
    }, 
    success: function(response) { 
     console.log(response); 
    } 
}) 
+0

问题出在服务器端,使用Jquery不是任何解决方案。 –