14

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);
}
random
  • 9,774
  • 10
  • 66
  • 83
kennyisaheadbanger
  • 488
  • 1
  • 4
  • 19
  • Hi Joe, I'm interested what was your solution? Did you find one? My workaround to this prob was assigning onload event instead of onreadystatechange (see details below in answers). – Svitlana Maksymchuk Jun 08 '09 at 06:17
  • i dunno.... sorry i haven't been on the site for a while. well it worked but I just switched to jQuery and it's working properly. – kennyisaheadbanger Jul 11 '09 at 13:47

5 Answers5

5

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);
}
Ross
  • 46,186
  • 39
  • 120
  • 173
Svitlana Maksymchuk
  • 4,330
  • 2
  • 16
  • 11
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)

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
1

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.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • 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 – kennyisaheadbanger Apr 15 '09 at 13:27
  • 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 Apr 15 '09 at 14:46
  • Furthermore: my readystatechange handler is defined *after* the send method is called. – KooiInc Apr 15 '09 at 14:50
0

I also faced the same issue. By reading the url below, I got mine solved.

http://bytes.com/topic/javascript/answers/548442-ajax-readystate-1-wall

basically, when I assign my function as the event listener for httpRequest.onreadystatechange, I cannot pass any variable to it. SO that I have to embed the variable inside the HTTP POST string to the server backend then get it back from the HTTP response.

It works fine for FF 3. No need to use jQuery.

user443461
  • 41
  • 6
0

I had the same problem on FireFox but not on Chrome.

The problem was my response had the mime-type set to "application/octet-stream".

Changing it to "text/html" made it work on FireFox too.

Panu Logic
  • 2,193
  • 1
  • 17
  • 21