1

I have a function which does get requests using plain old JS.

function get(url,funct){
    var xmlhttpget = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttpget.onreadystatechange=function(){
        if (xmlhttpget.readyState==4 && xmlhttpget.status==200){
            funct(xmlhttpget.responseText);
        }
    }
    xmlhttpget.open("GET",url,true);
    xmlhttpget.send();
}

I use it like this:

get(url, function(resp){ window.alert(resp); });

When my callback function stopped working I modified my get() function to the following for debugging:

function get(url,funct){
    var xmlhttpget = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttpget.onreadystatechange=function(){
        window.alert("ready state: "+xmlhttpget.readyState+" // status: "+xmlhttpget.status);
        if (xmlhttpget.readyState==4 && xmlhttpget.status==200){
            window.alert("responded...");
            funct(xmlhttpget.responseText);
        }
    }
    window.alert(url);
    xmlhttpget.open("GET",url,true);
    xmlhttpget.send();
}

With this I discovered that, during each readystate change the readystate will progress from 1 to 2 to 4, but the status always stays at 0 so my callback is never called, however, when I copy the alerted url to the browser it works fine.

What's going on? Is my browser caching the page? How can I fix it (set a specific header on the requested page with php??)?

Oh, not sure if this is relevant, but the page that's calling the function is inside of a frame whose parent document includes the same JavaScript file.

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116

1 Answers1

0

Try printing the statusText of the object. You may run into the browsers security policy.

Zenorbi
  • 2,504
  • 2
  • 15
  • 18