简体   繁体   English

Ajax:未从XML文件检索数据

[英]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中找到该错误:)

  1. Check that the function MakeXMLHTTPCall() is actually called. 检查是否确实调用了函数MakeXMLHTTPCall()。 place an alert(1); 发出警报(1); statement in the top of the function, and click the button. 函数顶部的语句,然后单击按钮。 If you don't get a popup with the text '1', then this function isn't even called. 如果没有出现带有文本“ 1”的弹出窗口,则甚至不会调用此函数。 This is most likely due to syntax errors somewhere in your javascript. 这很可能是由于javascript中某处的语法错误所致。
  2. Now that you have determined, that MakeXMLHTTPCall() gets called, when you click the button, check your if-statement. 既然您已经确定,将使MakeXMLHTTPCall()被调用,当您单击按钮时,检查您的if语句。 Place alert(1); 放置警报(1); as the first thing in the if-statement. 作为if语句中的第一件事。 If this don't bring a popup, then your xmlHttpObj isn't created properly. 如果没有弹出,则说明您的xmlHttpObj创建不正确。 So check your CreateXmlHttpRequestObject() to see that it's working correctly. 因此,请检查您的CreateXmlHttpRequestObject()以确保其正常工作。
  3. Check that the url you're fetching actually exists. 检查您要提取的网址是否确实存在。 And you will find, that you accidentally have a space misplaced. 您会发现,您不小心放错了空间。 That is "http:// " should be "http://" 即“ http://”应为“ http://”

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM