9
I have a global ajax error handler that runs even though the xhr.status is 200, xhr.statusText is "OK" and xhr.responseText is my JSON string. This happens in firefox and IE.
$.ajax({
data: {
method: "getRequestDetails",
loggedInUsername: loggedInUsername,
search: search
},
success: function(data){
var arrayObject = eval("(" + data + ")")['DATA'];
if (arrayObject.length == 0){
alert("That search term returned no results");
} else {
callBeforeShow("Results");
$.each(arrayObject, function(index, value){
showJSON(value, "Results");
});
callAfterShow("Results");
}
}
});
$(document).ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError){
var errorMessage = "Ajax Error\n";
errorMessage += "Type: " + ajaxOptions.type + "\n";
errorMessage += "Requesting Page: " + ajaxOptions.url + "\n";
errorMessage += "Status: " + XMLHttpRequest.status + " - " + XMLHttpRequest.statusText + "\n";
errorMessage += "Error Thrown: " + thrownError
alert(errorMessage);
});
In IE this says that the XMLHttpRequest is not ready and in Firefox this returns
"AJAX Error" "Type: POST" "Requesting Page: something.CFC" "Status: 200 - OK" "Error Thrown: undefined"
So my work around is to use
$(document).ajaxComplete(function(event, XMLHttpRequest, ajaxOptions, errorThrown){
if (XMLHttpRequest.status != 200){
var errorMessage = "Ajax Error\n";
errorMessage += "Type: " + ajaxOptions.type + "\n";
errorMessage += "Requesting Page: " + ajaxOptions.url + "\n";
errorMessage += "Status: " + XMLHttpRequest.status + " - " + XMLHttpRequest.statusText;
alert(errorMessage);
}
});
EDIT *This only happens on some occassions. Most of the time it works but sometimes it runs $.ajaxError()