Heylow everyone!
I have an ajax()
call like so:
$.ajax({
type: "post",
url: "whatever.php",
data: {
theData: "moo moo"
},
success: function(data) {
console.log(data);
}
});
Is it possible to wrap this inside a custom function but retain the callback?
Something like:
function customAjax(u, d, theCallbackStuff) {
$.ajax({
type: "post",
url: u,
data: d,
success: function(data) {
//RUN theCallbackStuff
}
});
}
theCallbackStuff
will be something like:
var m = 1;
var n = 2;
alert(m + n + data);
On this note, you can pass a complete function as a callback to this:
Simply passing it as an anonymous function will work too.. Just for the sake of showing :)
EDIT:
Got a recent upvote for this and I feel compelled to state that I would no longer do it this way.
$.ajax
returns apromise
so you can do pretty much what i just did here in a more consistent and robust way using the promise directly.Then usage looks like:
Sure i do this all the time. You can either execute the callback within the actual success callack or you can assign the callback as the success callback:
Usage would look something like: