Ajax won't get past readyState 1, why?

53,150

Solution 1

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);
}

Solution 2

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)

Solution 3

Possibly the Ajax request doesn't return data (so, a server side error of some kind). Try enabling the option 'show XMLHttpRequests' in the firebug console, to check for this.

Share:
53,150
kennyisaheadbanger
Author by

kennyisaheadbanger

Hi, my name's Joe and i am an budding young programmer, but I get stuck occasionally on my projects.

Updated on September 02, 2020

Comments

  • kennyisaheadbanger
    kennyisaheadbanger almost 4 years

    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);
    }
    
  • kennyisaheadbanger
    kennyisaheadbanger about 15 years
    Well it's already on and I log the http object and inspecting it gives me readyState 4, but the event is not being called. Also the page returns data properly
  • KooiInc
    KooiInc about 15 years
    Tried logging my own xmlHTTP lib function in FF, no problem. In my case I use => if (httpRequest.readyState < 4) {console.log(...);} else { [exec the callback]}.
  • KooiInc
    KooiInc about 15 years
    Furthermore: my readystatechange handler is defined after the send method is called.
  • Mirko Cianfarani
    Mirko Cianfarani almost 10 years
    Error console: Uncaught ReferenceError: isFirefox is not defined