Passing additional parameters to $.ajax complete callback function

I'm writing a jQuery plugin where I need to use an $.ajax request and to handle the callback with additional parameter but I find it very difficult to implement it.

a semplified version:

$('#element_id').click (function () {
    var additional_params = { name:'mike', surname:'buongiorno' };
    var ajax_params = {
        type: "POST",
        url: "some.php",
        data:'data here',
        complete:getResponse
    }
    sendAJAX (ajax_params, additional_params);
});
var sendAJAX = function (ajax_params, additional_params) {
    $.ajax(ajax_params, additional_params);
}

var getResponse = function (data, additional_params) {

}

Does exist some way to do something like this to pass additional_params to complete callback function (in this case getResponse)?

Answers


function callback(stuff){
   // use stuff
}

...

   var something="something";

   $.ajax({
      url: "test.html",
      complete: function(){
         callback(something);
      }
   });

...

And in case you need one of the parameters that already get passed you can change it to this:

complete: function(jqXHR, textStatus){
      callback(jqXHR, textStatus, something);
}

You can do something like:

$('#element_id').click (function () {
    var additional_params = { name:'mike', surname:'buongiorno' };
    var ajax_params = {
        type: "POST",
        url: "some.php",
        data:'data here',
        complete:getResponse(additional_params)
    }
    sendAJAX (ajax_params, additional_params);
});


var getResponse = function(additional_params) {
    return function(data) {
        //code that uses 'additional_params' (and 'data') can go here
        alert(additional_params.name);
    };
};

Basically getResponse has been modified so that it returns a function closure that includes whatever additional_params you passed in. The parameters will be available to the handler function when jQuery invokes it.


Here's the pasttern of code I use to post JSON data, and then process the response using a callback method that allows me to setup some data in 'info' that will be available asynchronously, and passed into the callback method as the second parameter to the callback method. This way your callback methods can have some 'context' passed into them (via 'info'), and it wraps all the 'boilerplate' in this method called 'json'. The key line is of course the place where it calls the callback method. Thanks to the post by James Montagne, which helped me come up with this design.

    json : function(postName, postData, callback, info) {
        if (typeof callback !== "function") {
            console.log("callback not valid function for postName "
                    + postName);
        }

        // console.log("JSON-POST: " + JSON.stringify(postData));
        $.ajax({
            url : postTargetUrl + postName,
            contentType : "application/json",
            type : "post",
            dataType : "json",
            cache : false,
            data : JSON.stringify(postData),
            success : function(jqXHR, textStatus) {
                // console.log("JSON-RESULT: "+postName+" -> " +
                // textStatus);
                if (textStatus === "success") {
                    callback(jqXHR, info);
                } else {
                    console.log("JSON: " + postName + " -> " + textStatus);
                }
            }
        });
    }

Need Your Help

one-sided Poisson confidence interval in python

python statistics poisson

Given this code that was given as an answer in another question:

C# Reloading form

c# winforms forms reload

When user changes background color for example, the Settings.settings file is modified. And it works.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.