JQuery : Additional parameter to be passed in $.ajax callback function


Just a quick snippet.. if you want to pass additional parameters to be used in the callback function of an ajax request using JQuery… here is an example:


var foo = 20;
$.ajax({
url : 'http://someurl.com',
success : passingAdditionalParameter(foo),
error : error
});

//in this function declaration, return an anonymous
//function with your logic within
function passingAdditionalParameter(foo){
return function(data, textStatus, jqXHR){
//foo is now available in the callback function
console.log(foo);
}
}

function error(){
console.log('error');
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s