當談到創建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