views:

35

answers:

3

When $.post succeeds, there is a success handler for it. What happens if it fails? Is there a similar handler that we can use for this case, so that we can inform the user that something is not happening right?

+1  A: 

Why use jQuery for straight AJAX? Use the standard directly and bind to readystatechange - then check for status and work from there.

Delan Azabani
+3  A: 

There is not, according to the documentation, a specific error handler for $.post method.

What you have to do, if you want to have both the success and fail handlers, is to use the low-level $.ajax method. It's documentation can be found here: http://api.jquery.com/jQuery.ajax/

$.ajax({
  type: "POST",
  url: "some.php",
  success: function(html){
    /* Do success stuff here */
  },
  error: function(){
    /* do error stuff here */
  }
});
Alastair Pitts
Thanks guys!!!!!! This is really useful!!!
Jux
A: 

You can catch it using .ajaxError(), but this applies to all ajax requests in your application. You also need to make sure you send back a HTTP error status back to the front end to be captured by jQuery.

jpartogi