AJAX responseXML错误

AJAX responseXML错误

问题描述:

在处理AJAX请求和处理响应时,我遇到了一些奇怪的问题。

I've been having some weird issues when it comes to make an AJAX request and handling the response.

我正在为xml文件调用ajax。但是当我得到响应xhr.responseText属性工作正常在Firefox,但不是在IE。另一件事是,我试图访问xhr.responseXML作为XMLDocument,但它告诉我在firefox它告诉我,xhr.responseXML是未定义的,即它甚至不显示未定义的错误或显示输出。

I am making an ajax call for an xml file. however when i get the response the xhr.responseText property works fine in firefox but not in IE. Another thing is that I am trying to access the xhr.responseXML as XMLDocument, but it tells me in firefox it tells me that xhr.responseXML is undefined in ie it doesnt even show me the undefined error or displays the output.

这是我用来提出请求的代码:

This is the code I am using to make the request:

var ajaxReq = function(url, callback) {
    //initialize the xhr object and settings
    var xhr = window.ActiveXObject ?
            new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(),
    //set the successful connection function
        httpSuccess = function(xhr) {
            try {
                // IE error sometimes returns 1223 when it should be 204
                //  so treat it as success, see XMLHTTPRequest #1450
                // this code is taken from the jQuery library with some modification.
                return !xhr.status && xhr.status == 0 ||
                        (xhr.status >= 200 && xhr.status < 300) ||
                        xhr.status == 304 || xhr.status == 1223;
            } catch (e) { }
            return false;
        };

    //making sure the request is created
    if (!xhr) {
        return 404; // Not Found
    }


    //setting the function that is going to be called after the request is made
    xhr.onreadystatechange = function() {
        if (!httpSuccess(xhr)) {
            return 503; //Service Unavailable
        }
        if (xhr.responseXML != null && xhr.responseText != null &&
                xhr.responseXML != undefined && xhr.responseText != undefined) {
            callback(xhr);
        }
    };


    //open request call
    xhr.open('GET', url, true);

    //setup the headers
    try {
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.setRequestHeader("Accept", "text/xml, application/xml, text/plain");
    } catch ( ex ) {
        window.alert('error' + ex.toString());
    }

    //send the request
    try {
        xhr.send('');
    } catch (e) {
        return 400; //bad request
    }

    return xhr;
};

这是我如何调用函数来测试结果:

and this is how i am calling the function to test for results:

window.onload = function() {
    ajaxReq('ConferenceRoomSchedules.xml', function(xhr) {
        //in firefox this line works fine,
        //but in ie it doesnt not even showing an error
        window.document.getElementById('schedule').innerHTML = xhr.responseText;
        //firefox says ''xhr.responseXML is undefined'.
        //and ie doesn't even show error or even alerts it.
        window.alert(xhr.reponseXML.documentElement.nodeName);
    });
}

这也是我第一次尝试使用AJAX,我不是在看权利。
我一直在寻找疯狂的任何迹象,为什么或如何解决它,但没有运气。
任何想法都会很棒。

This is also my first attempt to work with AJAX so there might be something that I am not looking at right. I've been searching crazy for any indications of why or how to fix it, but no luck there. any ideas would be great.

编辑

知道这将是更好的一个框架,但老板不想添加一个框架只是一个ajax功能('只是'不是一个公正的词ajax:P)。所以我使用纯JavaScript。

I know this would be better with a framework, but the boss doesn't want to add a framework for just an ajax functionality ('just' is not a fair word for ajax :P). So I am doing it with pure javascript.

XML文件格式良好,我在网络浏览器中看得很好,但是完成这是测试文件使用:

The XML file is well-formed, I see it well in the web browser, but for completion this is the testing file I am using:

<?xml version="1.0" encoding="utf-8"?>
<rooms>
  <room id="Blue_Room">
    <administrator>somebody@department</administrator>
    <schedule>
      <event>
        <requester>
          <name>Johnny Bravo</name>
          <email>jbravo@department</email>
        </requester>
        <date>2009/09/03</date>
        <start_time>11:00:00 GMT-0600</start_time>
        <end_time>12:00:00 GMT-0600</end_time>
      </event>
    </schedule>
  </room>
  <room id="Red_Room">
    <administrator>somebody@department</administrator>
    <schedule>
    </schedule>
  </room>
  <room id="Yellow_Room">
    <administrator>somebody@department</administrator>
    <schedule>
    </schedule>
  </room>
</rooms>

EDIT 2:
好​​消息是说服我的老板使用jQuery,坏消息是,AJAX仍然困扰我。我会读更多的只是为了好奇。感谢你的提示,我给了他的答案信任Heat Miser,因为他是最接近的工作提示。

EDIT 2: Well the good news is that I convinced my boss to use jQuery, the bad news is that AJAX still perplexes me. I'll read more about it just for curiousity. Thanks for the tips and I gave the answer credit to Heat Miser because he was the closest working tip.

同样的问题几年前,然后我放弃了responseXML并开始总是使用responseText。这个解析函数一直为我工作:

I was having the same problem a few years ago, then I gave up on responseXML and began always using responseText. This parsing function has always worked for me:

function parseXml(xmlText){
    try{
        var text = xmlText;
        //text = replaceAll(text,"&lt;","<");
        //text = replaceAll(text,"&gt;",">");
        //text = replaceAll(text,"&quot;","\"");
        //alert(text);
        //var myWin = window.open('','win','resize=yes,scrollbars=yes');
        //myWin.document.getElementsByTagName('body')[0].innerHTML = text;
        if (typeof DOMParser != "undefined") { 
            // Mozilla, Firefox, and related browsers 
            var parser=new DOMParser();
            var doc=parser.parseFromString(text,"text/xml");
            //alert(text);
            return doc; 
        }else if (typeof ActiveXObject != "undefined") { 
            // Internet Explorer. 
        var doc = new ActiveXObject("Microsoft.XMLDOM");  // Create an empty document 
            doc.loadXML(text);            // Parse text into it 
            return doc;                   // Return it 
        }else{ 
            // As a last resort, try loading the document from a data: URL 
            // This is supposed to work in Safari. Thanks to Manos Batsis and 
            // his Sarissa library (sarissa.sourceforge.net) for this technique. 
            var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text); 
            var request = new XMLHttpRequest(); 
            request.open("GET", url, false); 
            request.send(null); 
            return request.responseXML; 
        }
    }catch(err){
        alert("There was a problem parsing the xml:\n" + err.message);
    }
}

使用此XMLHttpRequest对象:

With this XMLHttpRequest Object:

// The XMLHttpRequest class object

debug = false;

function Request (url,oFunction,type) {
    this.funct = "";
    // this.req = "";
    this.url = url;
    this.oFunction = oFunction;
    this.type = type;
    this.doXmlhttp = doXmlhttp;
    this.loadXMLDoc = loadXMLDoc;
}

function doXmlhttp() {
    //var funct = "";
    if (this.type == 'text') {
        this.funct = this.oFunction + '(req.responseText)';
    } else {
        this.funct = this.oFunction + '(req.responseXML)';
    }
    this.loadXMLDoc();
    return false;
}

function loadXMLDoc() {
    //alert(url);
    var functionA = this.funct;
    var req;
    req = false;

    function processReqChange() {
        // alert('reqChange is being called');
        // only if req shows "loaded"
        if (req.readyState == 4) {
            // only if "OK"
            if (req.status == 200) {
                // ...processing statements go here...
                eval(functionA);
                if(debug){
                    var debugWin = window.open('','aWindow','width=600,height=600,scrollbars=yes');
                    debugWin.document.body.innerHTML = req.responseText;
                }
            } else {
                alert("There was a problem retrieving the data:\n" +
                    req.statusText + '\nstatus: ' + req.status);
                if(debug){
                    var debugWin = window.open('','aWindow','width=600,height=600,scrollbars=yes');
                    debugWin.document.body.innerHTML = req.responseText;
                }
            }
            }
    }

    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
        try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
                try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    req = false;
                }
        }
    }



    if(req) {
        req.onreadystatechange = processReqChange;
        if(this.url.length > 2000){
            var urlSpl = this.url.split('?');
            req.open("POST",urlSpl[0],true);
            req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            req.send(urlSpl[1]);
        } else {
            req.open("GET", this.url, true);
            req.send("");
        }
    }
}

function browserSniffer(){
    if(navigator.userAgent.toLowerCase().indexOf("msie") != -1){
        if(navigator.userAgent.toLowerCase().indexOf("6")){
            return 8;
        }else{
            return 1;
        }
    }
    if(navigator.userAgent.toLowerCase().indexOf("firefox") != -1){
        return 2;
    }
    if(navigator.userAgent.toLowerCase().indexOf("opera") != -1){
        return 3;
    }
    if(navigator.userAgent.toLowerCase().indexOf("safari") != -1){
        return 4;
    }
    return 5;
}

授予,这是非常旧的代码,但它仍然为我工作一个我几年前建成的网站。我同意其他人,虽然我通常使用现在的框架,所以我不必使用这个代码或任何类似的东西。

Granted, this is very old code, but it is still working for me on a site I built a few years ago. I agree with everyone else though I typically use a framework nowadays so I don't have to use this code or anything like it anymore.

您可以在Request onreadystate函数中忽略拆分等的一些细节。如果它的长度超过一定长度,应该将请求转换为一个帖子,但我只是决定总是更好的做一个职位。

You can ignore some of the particulars with the split, etc... in the Request onreadystate function. It was supposed to convert the request to a post if it was longer than a certain length, but I just decided it was always better to do a post.