I’m trying to write a function which will act on an array of data passed into it using AJAX. It’s not doing anything, and the last thing left that I can find that I might be doing wrong is the first parameter of my .apply()
call (i.e. the owner of my underlyingFunction()
method).
Here’s my code:
function XMLfunction(targetElement,param1,param2) {
var workingArray = new Array(targetElement);
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var parser = new DOMParser();
var xml = parser.parseFromString(this.responseText,"text/xml");
for (var i = 0; i < xml.childNodes[0].childNodes.length; i++) {
workingArray.push(xml.childNodes[0].childNodes[i].innerHTML);
}
alert (workingArray); // workingArray looks exactly like it's supposed to
underlyingFunction().apply(this,workingArray); // underlyingFunction() works perfectly when called directly in a script
alert ("applied"); // XMLfunction() stops before it gets to this line
}
}
request.open("GET","myURL.php?param1="+param1+"¶m2="+param2,true);
request.send();
}
The first parameter of .apply()
here is this
, but I have tried using document
and, out of desperation, window
as well. What should it be? Or what else am I doing wrong?