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);
}
readyState. Holds the status of the XMLHttpRequest. 0: request not initialized. 1: server connection established. 2: request received.
readyState is an XMLHttpRequest property. There are five ready states of a request as listed below: readyState=0.
Having the readyState with a value of 3 it means that the current state is LOADING .
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);
}
Check that the URL in question does actually respond by visiting it directly in the browser.
Test with a different browser do you get the same result.
Use some form of HTTP monitor to watch the client to server conversation (my favorite is Fiddler)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With