passing parameters to XMLHttpRequest’s onreadystatechange function
Tuesday, Nov 27. 2007 – Category: Code
I’ve been smashing my head against this all day – but I finally got something working consistently and reliable, so I better damn well document it. This is as good a place as any, and hopefully it will be useful to others.
I needed to make an Ajax call, so I turned to my good friend XMLHttpRequest. One wrinkle was that I needed to pass in a parameter to it… so I tried:
var test = "bar";
req = new XMLHttpRequest();
req.open("GET", myURL, true);
req.foo = test;
req.onreadystatechange = function() {
if (this.readyState != 4)
return;
if (this.status == 200) {
alert(this.foo); // should print out "bar"
}
}
req.send(null);
For the most part this worked. Except every now and then… when it didn’t. Most annoyingly, it failed pretty consistently when I was trying to use it within a nested Ajax call (complicated code, don’t ask.. it’s not interesting).
I’m not sure why it doesn’t work to be honest. From my understanding, req.foo should just instantiate a new foo member variable of the XMLHttpRequest object I just created and set it to be referenced via ‘this.foo’ inside any member function. I’m guessing it’s something to do with the scoping of onreadystatechange being set to the reference of an anonymous function so the anonymous function isn’t actually part of the XMLHttpRequest object and thus doesn’t have access to its member variables. What’s frustrating is that it works most of the time. A consistent failure model would actually be more helpful here.
Anyway, enough blabbering, here’s what seemed to work for me:
var test = "bar";
req = new XMLHttpRequest();
req.open("GET", myURL, true);
req.onreadystatechange = function(foo) {
return function() {
if (this.readyState != 4)
return;
if (this.status == 200) {
alert(foo);
};
}(test);
}
req.send(null);
Now that works reliably for me 100% of the time.
Tags: Ajax, xmlhttprequest
No Trackbacks to “passing parameters to XMLHttpRequest’s onreadystatechange function”
14 Comments to “passing parameters to XMLHttpRequest’s onreadystatechange function”
-
Jon H Says:
November 28th, 2007 at 08:31I bet that in your first example, if you used:
alert (this.parentNode.Foo)
it would work.
this.Foo inside the scope of the anonymous function is scoped wrong.
-
Stephen Lau Says:
November 28th, 2007 at 23:18Nope, trying to do that just got me errors that “this.parentNode” didn’t exist (or had no attributes)
-
nico Says:
February 13th, 2008 at 23:21Thanks, that really saved me from a long night of trying to figure out some hideous hack.
Just out of curiosity: how on Earth did you came out with that????
-
Stephen Lau Says:
February 15th, 2008 at 09:26Glad it was helpful.
I’m honestly not sure how I came up with it. Probably through some combination of beer, and helpful colleagues. -
Todd dellagio Says:
March 31st, 2008 at 13:22Hey thanks for this post. I was having a very similar issue and that got resolved. Thanks lau.
-
Scott F Says:
August 27th, 2008 at 00:32Thanks a lot for your help. I was having the same scope problems.
-
Will Says:
September 13th, 2008 at 21:11Hi,
I tried this method – no joy.
Any ideas where I’m messing up? Thanks.
I’m trying to pass the variable ‘url’ down to the next function.
function getData(url) { var datatosend=”graphrequest=graphcounter1″; var req = new XMLHttpRequest(); req.open(“POST”, url, true); req.onreadystatechange = function(url) { return function() { document.getElementById(“zone1″).innerHTML = “0″; if(req.readyState == 4 && req.status == 200) { document.getElementById(“zone1″).innerHTML = req.responseText;
setTimeout(“getData(url)”, 3000); //refresh rate for graph (ms) } }(url); };
req.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”); req.send(datatosend); } -
Krishna Says:
September 22nd, 2008 at 08:26var req = initRequest(url); req.onreadystatechange = function() { if (req.readyState != 4) return; if (req.status == 200 ) { //alert(flagforsubmit); flagforsubmit = true;
loadXML(req.responseXML); } }; req.open("GET", url, true); req.send(null);if(flagforsubmit){ ///code is not executed } since the flagforsubmit is not true its is taking too much time for execute and when i click the button on which i have called the function is not working(no action) after pressing 3 /4 times the output will come,
-
Kosta Says:
November 28th, 2008 at 09:42Thank you very much for this hack. Let me avoid ugly global variables.
-
pirhotek Says:
September 6th, 2009 at 16:24you could have simply referenced req.foo, rather than this.foo, as req is still in scope inside the inner function.
-
Chetna Says:
February 25th, 2010 at 15:41i don’t think req.foo or this.foo , either is required. I used foo directly and it worked for me. I didn’t even have to pass any parameter.The point here is : You are introducing an inner function which is in the scope of the outer function.Hence, all the variables can be directly referenced.
-
Alice Says:
February 25th, 2010 at 15:45instead of this.foo you could have used ‘test’ directly!!! you are passing a value to someone who already has the value with him
-
Viju Says:
November 16th, 2011 at 23:02Cool trick! Thanks a tun.
-
Peter Says:
May 9th, 2012 at 20:20Thank you thank you thank you thank you thank you thank you thank you!!!!!!!!!!!!!!!!!!!
Leave a Reply
Recent posts
- remiss
(Thursday, Nov 8. 2012 – 1 Comment) - Gesture Lock
(Saturday, Nov 13. 2010 – 14 Comments) - ConnectIn 1.1.1 & HTC Sense UI
(Tuesday, Sep 28. 2010 – 38 Comments) - ConnectIn
(Saturday, Sep 25. 2010 – 62 Comments)
Categories
- Android
- Cars
- ChinaBlog
- Code
- Computers
- Development
- Food
- Football
- Grommit
- Linkage
- Movies&TV
- Music
- Musings
- OpenSolaris
- OpenSource
- Outdoors
- Pets
- Photos
- Quotage
- Rdio
- Songbird
- Sun
- Travel
Grommit
Mozilla
OpenSolaris
- alan burlison
- bonnie corwin
- eric boutilier
- glynn foster
- jim grisanzio
- mark nelson
- mike kupfer
- planet opensolaris
- stephen hahn
Songbird
Archives
- November 2012
- November 2010
- September 2010
- August 2010
- June 2010
- May 2010
- February 2010
- January 2010
- December 2009
- October 2009
- September 2009
- August 2009
- June 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- April 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003
- August 2003
- July 2003
- June 2003
- May 2003
- April 2003
- March 2003
- February 2003
- January 2003
- December 2002
- November 2002
- October 2002
- September 2002
- August 2002
- July 2002
- June 2002
- May 2002
- April 2002
- March 2002
