AJAXリクエストを作成してレスポンスを処理するには、私は奇妙な問題を抱えています。AJAX responseXMLエラー
xmlファイルのajax呼び出しを作成しています。しかし、私は応答を取得するxhr.responseTextプロパティは、Firefoxでうまく動作しますが、IEでは動作しません。 もう1つは、xhr.responseXMLにXMLDocumentとしてアクセスしようとしていますが、xhr.responseXMLが未定義であることをFirefoxに伝えます。つまり、未定義のエラーを表示したり、出力を表示したりしません。
これは私が要求を行うために使用していたコードです:
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と連携するためには、私が正しく見ていないことがあるかもしれません。 私はそれをどうやって修正するかの兆候は見逃せませんでしたが、そこに運がありません。 すべてのアイデアが素晴らしいだろう。
EDIT:
私は、これはフレームワークと良いだろうけど、上司はちょうど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>
EDIT 2: まあ良いニュース私は上司にjQueryの使用を納得させました。悪い知らせは、AJAXがまだ私に困惑しているということです。好奇心のためだけに私はそれについてもっと読むでしょう。ヒントをありがとうと私は彼が最も近い作業のヒントだったのでヒートミーサーに答えクレジットを与えた。
レスポンステキストはブラウザ間の環境で見た目がよく見えますが、私はこのメソッドを使用するつもりです。スクリプトのおかげで –
問題はありません、私は助けることができてうれしい! –
あなたの答えは、7年後の同様の問題の正しい方向に私を指摘しました:)私はIE9で.responseを使用していましたが、他のすべてのブラウザでは動作しませんでした。 .responseTextはすべてのブラウザで動作します。良い! – K48