【发布时间】:2013-06-15 20:02:19
【问题描述】:
阅读 Ajax 初学者书籍,第二个示例是从 php 文件中获取 XMl 数据。我已经被困了大约 2 个小时,现在谷歌搜索并阅读该网站对其他人类似问题的答案,我无法弄清楚
我的功能
var options;
function getOptions1(){
var XMLHttpRequestObject = new XMLHttpRequest();
XMLHttpRequestObject.open("GET", "http://localhost/AV/data.php", false); // this was "true" somewhere i read to set it to "false"
XMLHttpRequestObject.onreadystatechange = function(){
if (this.readyState != 4) return;
if (this.status == 200){
alert ("hi");
var xmlDocument = this.responseXML;
options = xmlDocument.getElementsByTagName("option"); // firefox tels me here "TypeError xmlDocument is null"
listOptions();
}
}
XMLHttpRequestObject.send(null);
}
这是 data.php 文件
<?xml version="1.0" encoding="UTF-8" ?> //i read to add that encoding in there - no help
<options>
<option>red</option>
<option>green</option>
<option>blue</option>
</options>
【问题讨论】:
-
您是否尝试过使用 Firebug、Chrome Inspector 或任何类似工具来调试您正在创建的请求?
-
responseXML is null 的可能重复项
-
@Shikiryu 这是不是 OP 遇到的同样问题。他的代码已经检查了其他代码存在的问题。
-
本杰明的回答解决了我的问题,非常感谢