error handling with .post()

Jquery

Jquery Problem Overview


I have to modify a project written by someone else. Because the code is a mess I can't really change this $.post() (or replace it by $.ajax()). What I need to do, is to know if the post returns something else then JSON and return it.

$.post('balbal.html', json, function(data) { ... my coude ... }, 'json')

I can see the post response in the console.log. Is there a simple way to retrieve it?

Jquery Solutions


Solution 1 - Jquery

Define the error callback immediately after the post. Note the semi-colon placement only at the end.

$.post('balbal.html', json, function(data) {
    // ... my code ...
})
.fail(function(response) {
    alert('Error: ' + response.responseText);
});

http://api.jquery.com/deferred.fail/

For older versions of jQuery (pre-1.8) Use .error instead with the same syntax.

Solution 2 - Jquery

There's a couple of ways to do it, but if you're not getting the error response available through the returned JSON data object, you can use $.ajaxSetup() to define defaults for all ajax calls through jQuery. Even though the $.post() method doesn't specify an error handler or callback in and of itself, the $.ajaxSetup() method will capture any ajax faults and execute the function you define for it.

Alternatively, there is also an $.ajaxError() function that will do the same sort of thing.

Note that this will not work if your JSON response is returned correctly but with a server-side error code. If you want to capture these sorts of cases, you might be best to look for them using the $.ajaxComplete() method

Solution 3 - Jquery

You can use $.ajax() ($.post is a shortcut that ends up calling it anyway), and define the error callback.

Or, if you really do not want to do that, call

$.ajaxError(function(){
   //error handler
})

before that $.post

Solution 4 - Jquery

you can do something like that

/ Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });
 
// Perform other work here ...
 
// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second finished" );
});

for more information visite: https://api.jquery.com/jquery.post/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmeoView Question on Stackoverflow
Solution 1 - JqueryhyprnickView Answer on Stackoverflow
Solution 2 - JqueryPhil.WheelerView Answer on Stackoverflow
Solution 3 - JqueryVictorView Answer on Stackoverflow
Solution 4 - JqueryMAXView Answer on Stackoverflow