JQuery: How to get HTTP status code from $ .ajax.error method?

I am using jQuery to create an AJAX request. I want to perform various actions regardless of whether the HTTP status code is a 400 error or a 500 error. How can I achieve this?

$.ajax({ type: 'POST', url: '/controller/action', data: $form.serialize(), success: function(data){ alert('horray! 200 status code!'); }, error: function(data){ //get the status code if (code == 400) { alert('400 status code! user error'); } if (code == 500) { alert('500 status code! server error'); } }, }); 

Update:

@GeorgeCummins mentioned that it "seemed strange" to work with the response body. This is the first time I have tried to do such things. Is my approach not a good practice? What would you suggest? I created another StackOverflow question for this here: What response / status code should be sent to the AJAX request when the user / form validation fails?

+57
jquery
Jul 14 2018-11-22T00:
source share
5 answers

If you are using jQuery 1.5, then statusCode will work.

If you are using jQuery 1.4, try the following:

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

You should see the status code from the first warning.

+79
Jul 14 '11 at 10:45
source

You must create an action map using the statusCode parameter:

 $.ajax({ statusCode: { 400: function() { alert('400 status code! user error'); }, 500: function() { alert('500 status code! server error'); } } }); 

Link (Scroll to: 'statusCode')

EDIT (in response to comments)

If you need to take an action based on the data returned in the response body (which seems strange to me), you will need to use error: instead of statusCode:

 error:function (xhr, ajaxOptions, thrownError){ switch (xhr.status) { case 404: // Take action, referencing xhr.responseText as needed. } } 
+49
Jul 14 '11 at 22:44
source

using

  statusCode: { 404: function() { alert('page not found'); } } 

-

 $.ajax({ type: 'POST', url: '/controller/action', data: $form.serialize(), success: function(data){ alert('horray! 200 status code!'); }, statusCode: { 404: function() { alert('page not found'); }, 400: function() { alert('bad request'); } } }); 
+6
Jul 14 '11 at 22:44
source

Another solution is to use the response.status function. This will give you the http status returned by the ajax call.

 function checkHttpStatus(url) { $.ajax({ type: "GET", data: {}, url: url, error: function(response) { alert(url + " returns a " + response.status); }, success() { alert(url + " Good link"); } }); } 
+4
Oct 07 '15 at 7:36
source

Lorem Ipsum is just a fictitious text in printing and layout. Lorem Ipsum has been a standard industrial model since the 1500s, when an unknown printer took a set of fonts and scrambled it to make a model book. He survived not only five centuries, but also a leap in electronic typing, while remaining virtually unchanged. It was popularized in the 1960s with the release of Letraset sheets containing

0
Dec 13 '18 at 12:26
source



All Articles