开发者

jQuery $.ajaxError() runs on 200 - OK

开发者 https://www.devze.com 2023-01-29 03:55 出处:网络
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.

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 Min开发者_JAVA百科utes - 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.


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.


Could it be because you are not telling $.ajax that you are expecting JSON back from the server? Try setting the dataType option to 'json':

$.ajax({
    dataType: 'json',
    data: { ...


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

2 x comma missing between }{.


ah, well after days of searching i commented out different settings in $.ajaxSetup(). It ended up being that i had set a timeout. If only there was a better error message given. Thanks for all the help guys, would not have gotten here if not for you. You all get up votes!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消