I am having some strange problems when it comes to AJAX request and handling the response.
I am making an ajax call to an XML file. however, when I get the answer, the xhr.responseText property works fine in firefox, but not in IE. Another thing is that I'm trying to access xhr.responseXML as an XMLDocument, but it tells me that in firefox it tells me that xhr.responseXML is undefined, i.e. it doesn't even show me an undefined error or displays the result.
This is the code I use to execute the request:
var ajaxReq = function(url, callback) {
var xhr = window.ActiveXObject ?
new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(),
httpSuccess = function(xhr) {
try {
return !xhr.status && xhr.status == 0 ||
(xhr.status >= 200 && xhr.status < 300) ||
xhr.status == 304 || xhr.status == 1223;
} catch (e) { }
return false;
};
if (!xhr) {
return 404;
}
xhr.onreadystatechange = function() {
if (!httpSuccess(xhr)) {
return 503;
}
if (xhr.responseXML != null && xhr.responseText != null &&
xhr.responseXML != undefined && xhr.responseText != undefined) {
callback(xhr);
}
};
xhr.open('GET', url, true);
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());
}
try {
xhr.send('');
} catch (e) {
return 400;
}
return xhr;
};
and this is how I call the function to check the results:
window.onload = function() {
ajaxReq('ConferenceRoomSchedules.xml', function(xhr) {
window.document.getElementById('schedule').innerHTML = xhr.responseText;
window.alert(xhr.reponseXML.documentElement.nodeName);
});
}
This is also my first attempt to work with AJAX, so there may be something that I am not looking right at. I was looking crazy for any directions on why and how to fix it, but no luck. any ideas would be great.
EDIT:
, , ajax ( "" ajax: P). javascript.
XML , -, , :
<?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>
2:
, jQuery, , AJAX . . , Heat Miser, .