Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  58] [ 1]  / answers: 1 / hits: 55237  / 15 Years ago, wed, april 15, 2009, 12:00:00

I'm trying to get this function to work, which does a request for parameter url then sends the responseText to callback which is a function.



It seems that it only gets to readyState 1 (thanks to the Firebug commands).



Here it is:



function Request(url, callback){
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
httpRequest = new ActiveXObject(Microsoft.XMLHTTP);
} else{
return false;
}
httpRequest.onreadystatechange = function(){
console.log(httpRequest.readyState);
if (httpRequest.readyState == 4) {
callback(httpRequest.responseText);
}
};
console.log(httpRequest, url);
httpRequest.open('GET', url, true);
httpRequest.send(null);
}

More From » ajax

 Answers
26

I workarounded this problem assigning onload event instead of onreadystatechange:



function Request(url, callback){
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
httpRequest = new ActiveXObject(Microsoft.XMLHTTP);
} else{
return false;
}

var readyStateChange = function(){
console.log(httpRequest.readyState);

if (httpRequest.readyState == 4) {
callback(httpRequest.responseText);
}
};


if (isFirefox && firefoxVersion > 3) {
httpRequest.onload = readyStateChange;
} else {
httpRequest.onreadystatechange = readyStateChange;
}

console.log(httpRequest, url);
httpRequest.open('GET', url, true);
httpRequest.send(null);
}

[#99701] Wednesday, April 8, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;