20 December 2017

Snippet shows you how to catch errors like 404 when you submit form using jQuery.

Source code viewer
  1. $form.submit(function(e) {
  2. e.preventDefault();
  3.  
  4. // Pre-request code, like showing loader...
  5.  
  6. $.post($form.attr('action'), $form.serialize())
  7. // Successful request.
  8. .done(function(data) {
  9. // For an example, show success message.
  10. })
  11. // An error occurred during the request.
  12. .fail(function() {
  13. // For an example, show error.
  14. })
  15. // This method is always executed.
  16. .always(function() {
  17. // For an example, remove loader.
  18. });
  19. });
Programming Language: jQuery