2

Jquery provides methods for handling server response like done and fail. I'm wondering which status codes returned by the server are handled by callback passed to done and which are handled by callback passed to fail methods? Apperantly, the status code 200 is handled by done callback, and status code 500 is handled by fail callback. What about the others?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

2 Answers2

3

From the jQuery source code:

isSuccess = status >= 200 && status < 300 || status === 304;

So 2xx or 304 code is successful, anything else is failure.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

Use:

if ( status >= 200 && status < 300 || status === 304 ) {
   //success
}else{
   //failed
}

You can even handle the response based on status.

request = $.ajax({
type: "GET",
url: url,
data: data,
complete: function(e, xhr, settings){
   if(e.status === 200){

   }else if(e.status === 304){

   }else{

   }
   )};
  )};
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125