AJAX Response 0


AJAX Response 0

自从大学以来,我从未真正做过太多的网络开发,但现在我需要建立一个小网站来显示一些数据库数据。我认为AJAX将是实现这一点的最佳方式。

因此,我尝试了一个非常简单的任务来了解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上的某个人能够看到我忽略的东西。

提前谢谢。

您可以使用jQuery Ajax调用来代替JavaScript:

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