Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.ajaxError() runs on 200 - OK

Tags:

jquery

ajax

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()* EIDT

{"COLUMNS":["ID","SUMMARY_SPEC","TOTAL_EFFORT","EFFORT_HISTORY","LOG_HISTORY"],"DATA":[[816,"test only","2 Minutes - Last Updated: 09\/12\/2010",{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","EFFORT_DATE","EFFORT_DAYS","EFFORT_HRS","EFFORT_MINS","EFFORT_TOT_HRS"],"DATA":[[816,496,"ruhlet","Tim Ruhle","December, 09 2010 00:00:00",0,0,1,0.0167],[816,497,"ruhlet","Tim Ruhle","December, 08 2010 00:00:00",0,0,1,0.0167]]},{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","LOG_DT","LOG_ENTRY"],"DATA":[]}]]}{"COLUMNS":["ID","SUMMARY_SPEC","TOTAL_EFFORT","EFFORT_HISTORY","LOG_HISTORY"],"DATA":[[816,"test only","2 Minutes - Last Updated: 09\/12\/2010",{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","EFFORT_DATE","EFFORT_DAYS","EFFORT_HRS","EFFORT_MINS","EFFORT_TOT_HRS"],"DATA":[[816,496,"ruhlet","Tim Ruhle","December, 09 2010 00:00:00",0,0,1,0.0167],[816,497,"ruhlet","Tim Ruhle","December, 08 2010 00:00:00",0,0,1,0.0167]]},{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","LOG_DT","LOG_ENTRY"],"DATA":[]}]]}{"COLUMNS":["ID","SUMMARY_SPEC","TOTAL_EFFORT","EFFORT_HISTORY","LOG_HISTORY"],"DATA":[[816,"test only","2 Minutes - Last Updated: 09\/12\/2010",{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","EFFORT_DATE","EFFORT_DAYS","EFFORT_HRS","EFFORT_MINS","EFFORT_TOT_HRS"],"DATA":[[816,496,"ruhlet","Tim Ruhle","December, 09 2010 00:00:00",0,0,1,0.0167],[816,497,"ruhlet","Tim Ruhle","December, 08 2010 00:00:00",0,0,1,0.0167]]},{"COLUMNS":["CC_ID","RECORD_ID","USER_ID","USER_NAME","LOG_DT","LOG_ENTRY"],"DATA":[]}]]}

The latest version of firebug recognises it as json.

like image 420
Timothy Ruhle Avatar asked Dec 08 '10 00:12

Timothy Ruhle


People also ask

What is status 200 in ajax?

Ajax- status 200, without data, in response is NOT considered as request success #3098.

Why is ajax returning error?

In ASP.NET MVC if you call an action method on the server from JavaScript code and that action method raises an exception, you'll get an error at the client that lets you know something has gone wrong.


1 Answers

First off, I'd highly recommend ditching the eval part in favor of using $.parseJson or the dataType:'json' ajax option that automatically handles the parsing (for reasons of performance and security among other things). If you do continue to use eval, at the very least wrap it it a try catch.

I'm not entirely sure of every case where ajaxError gets called but I suspect (based of the the "randomness" of the error) that it has to do with an error inside the success function (like eval being called on invalid javascript that you're getting in your response). This would explain why it is getting called even when there is a 200 response.

tldr: Get eval out of that success callback and assert that your responses are in fact valid json when this error occurs.

like image 57
nategood Avatar answered Oct 14 '22 20:10

nategood