Alex Rivera | Logout

jQuery $.ajaxError() runs on 200 - OK

Asked 2010-12-08T00:01:08.197
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()

Edit
Report

1 Answer

1

I get two errors when I copy&paste that json to this formatter/validator http://jsonformatter.curiousconcept.com/

2 x comma missing between }{.

answered 2010-12-15T22:40:36.223

Your Answer