Innovative Web Solutions
Blue Star have a proven reputation for building
high performance web designs and web applications
backed up by strategic online marketing campaigns.

Diarmuid Ryan & Associates

Ajax.request() - calling onSuccess handler with extra params

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.

Post to Twitter Post to Facebook Post to MySpace

Tags:

Leave a Reply


LinkedIn ProfileBecome Fan on FacebookFollow us on Twitter
Blue Star Web Design Ireland
e: sales@bluestar.ie
t: +353 86 3318412
Tipperary Town

Copyright ©2010 Blue Star