1

Firstly, here's my example code:

$.post( "test.php", { testdata : "signal" },
            function( data ) { alert( "Nested function" )})
 .done( function () { alert( "Done function" )});

I'm trying to get .done to accomplish some stuff for me, it doesn't matter what. It won't fire, regardless of whether or not I nest a function directly in the $.post parameters. Checking the console on Chrome shows that it's throwing an error of Uncaught TypeError: undefined is not a function

Playing around with lines and the error report shows me that it's definitely the .done that's throwing the error, and that simply moving the .done to directly after $.post's close paren isn't effective.

There is another similar question that I saw, but it was for $.ajax, and I've attempted to implement the chaining they suggest, as above, to no effect. What am I missing?

EDIT: jQuery version is 1.4.4, the .post successfully returns data, and .always also fails.

Community
  • 1
  • 1
Emmett R.
  • 145
  • 9
  • 1
    Are you inside a document ready function and sure that the jQuery library has loaded successfully? What version of jQuery are you using? Is the post maybe failing and you're getting into the fail callback? Can you log something in the always callback? – Trent Jun 23 '14 at 20:24
  • Seems to work fine here http://jsfiddle.net/2LgMg/ – Patrick Q Jun 23 '14 at 20:26
  • I'm using jQuery 1.4.4. The post succeeds and returns data. `.always` also refuses to fire. – Emmett R. Jun 23 '14 at 20:30
  • 2
    @EmmettR. [Deferreds](http://api.jquery.com/jQuery.Deferred/) and [related methods](http://api.jquery.com/category/deferred-object/) were added with jQuery 1.5. You'll need to upgrade to use `.done()`, `.always()`, etc. – Jonathan Lonowski Jun 23 '14 at 20:31
  • Try property `success` in version 1.4.4 like this `$.post("test.php", { testdata: "signal", success: function(data) {}});` – algorhythm Jun 23 '14 at 20:32
  • .always is called 'always'. If the ajax call fails and if it succeeded – algorhythm Jun 23 '14 at 20:33
  • Hmm... I have to keep version 1.4.4 for compatibility. Would it be excessive to install both an updated and an outdated version on my server, or would that cause collisions? – Emmett R. Jun 23 '14 at 20:34
  • @EmmettR. It is possible. See [Can I use multiple versions of jQuery on the same page?](http://stackoverflow.com/questions/1566595/can-i-use-multiple-versions-of-jquery-on-the-same-page) Note: It will depend on plugins using [the suggested closures](http://learn.jquery.com/plugins/basic-plugin-creation/#protecting-the-alias-and-adding-scope). – Jonathan Lonowski Jun 23 '14 at 20:35
  • @algorhythm On on v1.5 and above. Before 1.5 you'll need to use the `complete` callback as deferreds were not yet supported. – War10ck Jun 23 '14 at 20:38

2 Answers2

0

Use .complete instead:

$.post("test.php", { testdata : "signal" }, function () { 
        alert("Nested function"); })
    .complete(function () { alert("Done function"); });

fiddle

lante
  • 7,192
  • 4
  • 37
  • 57
  • 1
    Note: `jqXHR.complete()` was introduced in 1.5 and [deprecated with 1.8](http://api.jquery.com/jQuery.ajax/#jqXHR). – Jonathan Lonowski Jun 23 '14 at 20:41
  • It's also not available in 1.4.4, as I tested it. – Emmett R. Jun 23 '14 at 20:42
  • @EmmettR. Are you sure? Check [here](http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js) and do a find (CTRL + F) for "ajaxComplete". It returns two event listings... – War10ck Jun 23 '14 at 20:48
  • @War10ck Though, that's not what this answer is referring to. `jqXHR.complete()` was a short-lived (1.5.x - 1.7.x) alias of `.always()` for `jqXHR` objects. – Jonathan Lonowski Jun 23 '14 at 21:58
0

So, after investigation, and thanks to the comments of Jonathan Lonowski, algorhythm and War10ck, I've discovered the solution.

The version of jQuery I was running, 1.4.4, does not support the .done, .fail, .always, or .complete* methods. To fire an event after a success, I need to follow the inline format of:

$.post( url [, data ] [, success ] [, dataType ] )

...inserting the function that would normally go into .done, after the url. This is what I was doing with the nested function in my original example, just requiring me to remove the .done function.

Thanks all.

*.complete is also deprecated in more recent builds of jQuery. Don't ever use it.

Emmett R.
  • 145
  • 9