I’m fairly new with jQuery, and I’m trying to call two functions on successful ajax (since the documentation says as of 1.5 success callback can take an array of functions).
If I do this, everything works fine:
$.ajax({
url : sJSONFilePath,
dataType : 'json',
success : foo(data)
});
What do I need to do to pass in an array of functions? If I try the following, I get a “Uncaught TypeError: Cannot read property ‘length’ of undefined” error in the console:
$.ajax({
url : sJSONFilePath,
dataType : 'json',
success : [foo(data), bar(data)]
});
I was unable to find any examples of this feature being used. Thanks in advance, and sorry if this is dumb.
There’s no need for an array, you can use the deferred syntax that was also introduced in jQuery 1.5:
This is generally cleaner and much more extensible than passing callback functions as parameters to
$.ajaxand relatives.From the
$.ajax()documentation:[but see the paragraph after, where it explains how
.success,.errorand.completeare now deprecated and replaced with.done,.failand.always]