MOCKSTACKS
EN
Questions And Answers

More Tutorials









Jquery Ajax


In addition to .done, .fail and .always promise callbacks, which are triggered based on whether the request was successful or not, there is the option to trigger a function when a specific HTTP Status Code is returned from the server. This can be done using the statusCode parameter.

$.ajax({
 type: {POST or GET or PUT etc.},
 url: {server.url},
 data: {someData: true},
 statusCode: {
 404: function(responseObject, textStatus, jqXHR) {
 // No content found (404)
 // This code will be executed if the server returns a 404 response
 },
 503: function(responseObject, textStatus, errorThrown) {
 // Service Unavailable (503)
 // This code will be executed if the server returns a 503 response
 } 
 }
})
.done(function(data){
 alert(data);
})
.fail(function(jqXHR, textStatus){
 alert('Something went wrong: ' + textStatus);
})
.always(function(jqXHR, textStatus) {
 alert('Ajax request was finished')
});


As official jQuery documentation states:

If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error (including 3xx redirect), they take the same parameters as the error callback.


Conclusion

In this page (written and validated by ) you learned about Jquery Ajax . What's Next? If you are interested in completing Jquery tutorial, your next topic will be learning about: Jquery Using Ajax to Submit a Form.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.