0

How can I check what the error value returned is in ajax callback error :

$.ajax({
            url: "myurl",       
            type: 'POST',
            dataType : "text",
            data : ({
                json : myjson
            }),
            success : function(data) {

            },
   error : function() {
                alert ('error');
    } 

        });  
blue-sky
  • 51,962
  • 152
  • 427
  • 752

2 Answers2

8

Try accessing these parameters for your error part of the ajax call:

error: function (request, status, error) {
        alert(request.responseText);

Another example:

error: function(xhr) {
                if(xhr.status == 422) {
                  alert(parseErrors(xhr));
                } else {
                  alert('An error occurred while processing the request.');
                }

They are part of the ajax call you seperate them with a comma. So a little example:

$.ajax({
  success: function(data) {
  },
  error: function(xhr) {
  }
  });

Update: Basically xhr.status is the http status number.

alert("readyState: "+xhr.readyState);
alert("status: "+xhr.status);
alert("responseText: "+xhr.responseText);
Pasha Immortals
  • 829
  • 1
  • 7
  • 19
  • when the error is thrown the value for xhr.status is 0 . Should it be something else if the error is valid ? – blue-sky Jun 18 '12 at 22:45
  • 1
    See the update section so you can see what else is in your returned messages for error, so we can see and put them in here. – Pasha Immortals Jun 18 '12 at 23:19
1

similar to this question

error : function(jqXHR, textStatus, errorThrown){
    alert(jqXHR.status);
}

http://api.jquery.com/jQuery.ajax/

Community
  • 1
  • 1
Jason Kulatunga
  • 5,814
  • 1
  • 26
  • 50