【发布时间】:2014-02-18 15:37:33
【问题描述】:
我希望 php 向 ajax 返回一个值。我正在关注 W3 学校的例子,但并不高兴。 这是 javascript/ajax 代码:
function createReport() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("report").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","test.php",true);
xmlhttp.send();
}
我知道正在触发的事件处理程序中有以下调用(它所做的其他事情工作正常)
createReport();
后来在我拥有的 html 的正文部分:
<div id="report">Report will be placed here...</div>
如果我自己运行 test.php,它会正确显示“将其返回给 ajax”,所以我知道这是有效的。这是php代码:
<?php
echo 'return this to ajax';
?>
我的理解是“Report will be placed here...”将替换为“return this to ajax”。但什么也没有发生。我也没有在 Firefox 或 IE 控制台中看到任何错误。有什么想法吗?
【问题讨论】:
-
很可能是你设置的顺序是HTTP错误处理的问题。看我的回答:stackoverflow.com/a/21387485/1518921
标签: javascript php ajax