Graeme MacFarlane
Graeme MacFarlane

Reputation: 217

How can I detect a jQuery ajax error response?

I am able to detect a successful response but not able to detect an error. My code:

$.ajax({
        url: url,
        dataType: 'jsonp',      
        success: function(data){
            $.each(data, function(a, b) { 
                if(b=="Processing post"){
                    $("#post_response").html('Your comment will be added soon...'); 
                }
                else if(b=="In moderation"){
                    $("#post_response").html('Your comment has been placed in moderation...');  
                }
            })
        },
        error: function(xhr, testStatus, error) {
            alert("error");
        }
    });

These are the responses I am receiving. I can detect the first 2 response but I cannot even alert "error" for the last one.

jQuery191004289518775843171_1361906819321({"Message":"Processing post","Code":202})

jQuery191006158838421090984_1361907415719({"Message":"In moderation","Code":202})

jQuery191006158838421090984_1361907415719({"Message":"Not logged in","Code":403})

Can you see where I'm going wrong?

Upvotes: 1

Views: 2972

Answers (1)

Steve
Steve

Reputation: 8640

The error callback is only used for server error responses (i.e. HTTP Status Code 4xx/5xx Errors). Since all three of your responses are valid (i.e. they return a HTTP Status Code of 200), they trigger the success callback.

Upvotes: 2

Related Questions