1

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)?

3 Answers 3

16
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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 this is the simplest solution. (especially the second code part where everybody just need data... and forget about jqXHR ). so why do i see the closure solutions ?
2

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.

Comments

0

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);
                }
            }
        });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.