Błędy odpowiedzi AJAX
Mam pewne dziwne problemy, gdy przychodzi do utworzenia żądania AJAX i obsługi odpowiedzi.
Wykonuję wywołanie ajax dla pliku xml. jednak gdy otrzymam odpowiedź, właściwość xhr.responseText działa poprawnie w Firefoksie, ale nie w IE. Inną rzeczą jest to, że próbuję uzyskać dostęp do xhr.responseXML jako XMLDocument, ale mówi mi on w firefox, że mówi mi, że xhr.responseXML jest niezdefiniowany, tzn. Nie pokazuje nawet niezdefiniowanego błędu lub wyświetla wyjście.
To jest kod, którego używam do złożenia wniosku:
<code>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; }; </code>
i tak dzwonię do funkcji, aby sprawdzić wyniki:
<code>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); }); } </code>
Jest to również moja pierwsza próba pracy z AJAX, więc może być coś, czego nie patrzę w prawo. Szukałem szaleństwa na wszelkie wskazania, dlaczego lub jak to naprawić, ale nie ma szczęścia. wszelkie pomysły byłyby świetne.
EDYTOWAĆ:
Wiem, że byłoby lepiej z frameworkiem, ale szef nie chce dodawać frameworka tylko dla funkcjonalności ajax („just” nie jest uczciwym słowem dla ajax: P). Robię to za pomocą czystego javascript.
Plik XML jest dobrze sformatowany, dobrze go widzę w przeglądarce internetowej, ale do uzupełnienia jest to plik testowy, którego używam:
<code><?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> </code>
EDYCJA 2: Dobrą wiadomością jest to, że przekonałem mojego szefa, aby używał jQuery, zła wiadomość jest taka, że AJAX wciąż mnie niepokoi. Więcej na ten temat przeczytam z ciekawości. Dziękuję za wskazówki i udzieliłem odpowiedzi na pytanie Heat Miser, ponieważ był on najbliższym wskazówką.