actaram
actaram

Reputation: 2058

XMLHttpRequest readyState is equal to 0

I can't get any data with XMLHttpRequest, because its readyState returns 0. I have this code:

function chargerArrondissements() {
    var xhrArrond = new XMLHttpRequest();
    xhrArrond.onreadystatechange = chargerArrondCallback(xhrArrond);
    var lienDocArrond = 'PHP/script_load_arrondissements_get.php';
    xhrArrond.open('GET', lienDocArrond, true);
    console.log('Arrondissements chargés dans le fichier xml');
    xhrArrond.send(null);
}

function chargerArrondCallback(xhrArrond) {
    alert(xhrArrond.readyState);
}

The alert window shows 0. I also tried opening the XHR before setting the handler, but it still return 0. Now, the strange thing is that I have many XHRs on that page, and they all work very well, except that one, and I can't explain why.

Btw, my php file path is: http://localhost/TP2/PHP/script_load_arrondissements_get.php .

Also, you should know that this file returns a correct xml content.

This is what the console says about my request: enter image description here

Anyone knows what could be the problem? Thanks.

Upvotes: 0

Views: 218

Answers (1)

Quentin
Quentin

Reputation: 943615

xhrArrond.onreadystatechange = chargerArrondCallback(xhrArrond);

You are calling your event handler function and assigning its return value (undefined) as the event handler.

Change it to:

xhrArrond.onreadystatechange = chargerArrondCallback;

… and then access the XHR object it is associated with using this inside the function.

Upvotes: 1

Related Questions