Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding on the jQuery post function

I have the following function in my code:

        $.post($form.attr('action'), $form.serializeArray())
            .done(function (json) {
            }

From what I understand from the jQuery docs this is a shortcut. What I would like to do is to change so that it allows me to have some function that executes on success and some function that executes on error. Is this possible to do? All I see is a .done?

$.ajax({
   url: target,
   dataType: 'json',
   type: 'POST',
   data: data,
   success: function(data, textStatus, XMLHttpRequest) { },
   error: function(XMLHttpRequest, textStatus, errorThrown) { }

2 Answers

Since all the jQuery ajax methods, including $.post(), return a jqXHR object, you can use the Deferred object API if you don't want to use a full-out $.ajax() call.

$.post(/* snip */).fail(function () {/* snip */});
like image 89
Matt Ball Avatar answered Jun 11 '26 19:06

Matt Ball


Actually you can use .success() .error() and .complete() as chained methods to .post() - http://api.jquery.com/jQuery.post/

like image 28
Jay Blanchard Avatar answered Jun 11 '26 19:06

Jay Blanchard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!