当谈到创建AJAX请求和处理响应时,我一直有一些奇怪的问题。AJAX响应XML错误
我正在为xml文件进行ajax调用。然而,当我得到的响应xhr.responseText属性在Firefox中正常工作,但不是在IE中。 另一件事是,我试图访问XMLDocument的xhr.responseXML,但它告诉我在Firefox中,它告诉我,xhr.responseXML是未定义的,即使它甚至不显示我未定义的错误或显示输出。
这是我使用发出请求的代码:
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;
};
,这就是我如何调用该函数测试结果:
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一起工作,所以可能有些东西我看起来不正确。 我一直在疯狂寻找为什么或如何修复它的任何迹象,但没有运气。 任何想法都会很棒。
编辑:
我知道这将是更好的框架,但老板不想添加一个框架,只是一个ajax功能(“只是”不是阿贾克斯公平的一句话: P)。所以我正在用纯JavaScript来做。
XML文件格式良好的,我看到它在Web浏览器很好,但完成这是我使用的测试文件:
<?xml version="1.0" encoding="utf-8"?>
<rooms>
<room id="Blue_Room">
<administrator>[email protected]</administrator>
<schedule>
<event>
<requester>
<name>Johnny Bravo</name>
<email>[email protected]</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>[email protected]</administrator>
<schedule>
</schedule>
</room>
<room id="Yellow_Room">
<administrator>[email protected]</administrator>
<schedule>
</schedule>
</room>
</rooms>
编辑2: 那么好消息是我说服我的老板使用jQuery,坏消息是AJAX仍然困扰着我。为了好奇,我会详细阅读它。感谢提示,我给了热吝啬的答案,因为他是最接近的工作提示。
这是一个很好的建议。让图书馆担心跨浏览器问题。 – 2009-06-18 16:10:42
我同意,我已经使用jQuery,但如果我们要使用ajax,我的老板不想用整个框架填充网站。所以我必须自己做。尽管我仍然试图说服他使用框架。 – 2009-06-18 16:32:05
@Tony L.,告诉他jQuery是一个脚本,而不是一个框架。框架听起来很重。 – 2009-06-18 16:54:48