Alex
Alex

Reputation: 1055

.post() failure callback function?

In the documentation for the .post() method, there's a callback function for when the request is successful, but is there a similar method to tell if the request was a failure?

Upvotes: 0

Views: 120

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

The $.post() which is a utility function built on top of $.ajax() returns a promise object which has a fail handler that can used to handle failure cases

$.post(url, yourdata, function(){
    //success handler
}).fail(function(){
    //fail handler
}).done(function(){
   //more success handlers if you want
})

Upvotes: 1

celeritas
celeritas

Reputation: 2281

Yes, use .fail:

$.post("test.php", function(data) {
    alert("Data Loaded: " + data);
})
.fail(function() { 
    alert("error"); 
})

Upvotes: 1

Related Questions