Recently I needed to pass extra parameters to the onSuccess handler of the Ajax.request() object. Having not found any answers elsewhere on the web or blogoshere I worked out a solution, tidy enough too (Javascipt is such a nuissance when it comes to debugging!).
params = 'servervar1=x&servervar2=y';
var req = new Ajax.Request('/cgi-bin/dosomething.cgi',{
onSuccess : function(r) { my_success_handler(r, var1, var2,..,var_n) },
method : "get",
parameters : params
});
}
function my_success_handler(response, var1, var2,..,var_n) {
var content = response.responseText;
//
// do handler function
}
Creating a function reference seems to allow adding passing of extra parameters, the key is the first parameter, ‘r’ - this is the response object from XMLHTTPObject which allows you to retrieve the text/xml object from the server request. Whatever client-side vars are needed for the handler can be used now.
This works in both IE and FireFox.
Tags: Web 2.0