When the response of an AJAX request is ready on the server the value of the readyState property of the XMLHttpRequest object is changed to “4″ which means the request is complete. The onreadystatechange property of the same object stores a user-defined anonymous function which is executed whenever the readyState is changed. A typical piece of code would be like:
xmlHttp.onreadystatechange=function () {
if(xmlHttp.readyState==4)
{
//do something with the response
}
};
However, sometimes it is required to pass one or more parameters to this anonymous function. It sounds tricky but it is pretty simple! This anonymous function can not take parameters but it can call another function defined in the same file and pass paramters to it. So, if you want to pass parameters to the anonymous function -you can do it as below:
xmlHttp.onreadystatechange=function () {
stageChanged(parameter1, parameter2);
};
xmlHttp.open("GET", handlingURL, true);
xmlHttp.send(null);
}
function stageChanged(p1, p2)
{
if(xmlHttp.readyState==4)
{
//do something with the response
}
}
GD Star Rating
loading...
AJAX: Passing parameters to onreadystatechange function,
loading...




