[英]Ajax: Data is not retrieved from XML file
I'm learning ajax and trying to display some data on a page: 我正在学习ajax并尝试在页面上显示一些数据:
This is my method that retrieves data from xml
file: 这是我从
xml
文件检索数据的方法:
function MakeXMLHTTPCall() {
var xmlHttpObj;
xmlHttpObj = CreateXmlHttpRequestObject();
if (xmlHttpObj) {
xmlHttpObj.open("GET", "http:// " + location.host + "/XmlHttpExample1/DataFile.xml", true);
xmlHttpObj.onreadystatechange = function () {
if (xmlHttpObj.readyState == READYSTATE_COMPLETE) {
document.getElementById("divResults").innerHTML = xmlHttpObj.responseText;
}
}
xmlHttpObj.send(null);
}
}
This is an html
fragment defining div
element that will hold data: 这是一个
html
片段,定义了div
元素,该元素将保存数据:
<form id="form1" runat="server" method="post">
<div>
<input type="button" onclick="MakeXMLHTTPCall();" value="Text XMLHTTP Call" />
<br />
<br />
<div id="divResults">{no results}</div>
</div>
</form>
This is my CreateXmlHttpRequestObject()
method: 这是我的
CreateXmlHttpRequestObject()
方法:
function CreateXmlHttpRequestObject() {
var xmlHttpObj;
if (window.XMLHttpRequest){
xmlHttpObj = new XMLHttpRequest();
}
else {
try {
xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
}
}
if (window.ActiveXObject) {
try{
xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
}
}
return xmlHttpObj;
}
var READYSTATE_UNINITILIZED = 0;
var READYSTATE_LOADING = 1;
var READYSTATE_LOADED = 2;
var READYSTATE_INTERACTIVE = 3;
var READYSTATE_COMPLETE = 4;
This is my xml
file: 这是我的
xml
文件:
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer>
<Firstname>John</Firstname>
<Lastname>Doe</Lastname>
<email>john.do@test.com</email>
</Customer>
<Customer>
<Firstname>Alan</Firstname>
<Lastname>Anonymous</Lastname>
<email>anon@ymous.com</email>
</Customer>
<Customer>
<Firstname>Marvin</Firstname>
<Lastname>Martian</Lastname>
<email>marvin@mars.com</email>
</Customer>
</Customers>
I debugged the code. 我调试了代码。
XMLHttpRequest
object is created. XMLHttpRequest
对象已创建。 The problem is, the data is not displayed. 问题是,数据不显示。
What am I doing wrong? 我究竟做错了什么? Any suggestions?
有什么建议么?
It sounds like you need to do a little debugging. 听起来您需要进行一些调试。 There's multiple things, that can go wrong in this setup.
有很多事情,在此设置中可能会出错。 So try to work through these steps, and determine how much of your code actually works.
因此,请尝试完成这些步骤,并确定实际上有多少代码可以工作。 You will find the bug in step 3 :)
您将在步骤3中找到该错误:)
I hope you didn't mind I gave you the explanation of how I found the bug. 我希望您不介意我给您解释了我如何发现该错误。 It is very frustrating not to know how to debug the code.
不知道如何调试代码非常令人沮丧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.