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?
Asked
Active
Viewed 1,580 times
2
Max Koretskyi
- 101,079
- 60
- 333
- 488
2 Answers
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
-
`)};` typo at the end. – Jai Feb 03 '14 at 06:30
-
Yes, thanks, I'm using the method `statusCode: {200 : onStatusCode200}` passed to `$.ajax`. – Max Koretskyi Feb 03 '14 at 06:31