2014-10-17 134 views
0

我不能使用XMLHttpRequest的任何數據,因爲它的readyState返回0。我有這樣的代碼:XMLHttpRequest的readyState爲等於0

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

警報窗口顯示爲0。我也試過開幕前XHR設置處理程序,但它仍然返回0.現在,奇怪的是,我在該頁面上有許多XHR,除了一個,它們都工作得很好,我無法解釋爲什麼。

順便說一句,我的PHP文件路徑是:http://localhost/TP2/PHP/script_load_arrondissements_get.php

另外,你應該知道這個文件返回一個正確的xml內容。

這是控制檯說什麼關於我的要求: enter image description here

任何人都知道可能是什麼問題呢?謝謝。

+0

您是否檢查過控制檯中的請求? – 2014-10-17 17:57:01

+0

是的,但我並不確定自己明白。我編輯了我的帖子。 – TheWanderingMind 2014-10-17 18:10:04

回答

1
xhrArrond.onreadystatechange = chargerArrondCallback(xhrArrond); 

您正在調用您的事件處理函數並將其返回值(undefined)指定爲事件處理函數。

將其更改爲:

xhrArrond.onreadystatechange = chargerArrondCallback; 

...然後訪問XHR對象是使用this裏面的功能有關。

+0

也許我不知道如何使用'this',但是我試過了: console.log(this.xhrArrond);在chargerArrondCallback函數內部,它返回undefined。 – TheWanderingMind 2014-10-17 18:27:33

+0

@ProgProsecutor - 你使用'this' *而不是''xhrArround',而不是它。 – Quentin 2014-10-17 18:33:12

+0

哦..我明白了..謝謝。 – TheWanderingMind 2014-10-17 18:44:10

相關問題