2013-10-19 33 views
3

就XMLHttpRequest()對象而言,它很好,問題出在onreadystatechange例如,如果我把我的代碼這樣,它的作品完美。onreadystatechange函數在AJAX中不工作

function process(){ 
    var xmlHttp = new XMLHttpRequest(); 

    if(xmlHttp){ 
     xmlHttp.onreadystatechange = function(){ 
      theD = document.getElementById("theD"); 
      if(xmlHttp.readyState == 1){ 
       theD.innerHTML += "Status 1: Server connection established ! <br/>"; 
       } 
      else if(xmlHttp.readyState == 2){ 
       theD.innerHTML += "Status 2: Request recieved ! <br/>"; 
       } 
      else if(xmlHttp.readyState == 3){ 
       theD.innerHTML += "Status 3: Processing Request ! <br/>"; 
       } 
      else if(xmlHttp.readyState == 4){ 

       if(xmlHttp.status == 200){ 
        var text = xmlHttp.responseText; 
        theD.innerHTML += "Status 4: Processing Request ! <br/>"; 
        theD.innerHTML += text; 
        } 
       else{ 
        alert("Something is wrong !"); 
        } 
       } 
      }; 
     xmlHttp.open("GET", "hello.txt", true); 
      xmlHttp.send(); 
     } 
} 

,但如果我做handleServerResponse()

function handleServerResponse(){ 
    theD = document.getElementById("theD"); 
      if(xmlHttp.readyState == 1){ 
       theD.innerHTML += "Status 1: Server connection established ! <br/>"; 
       } 
      else if(xmlHttp.readyState == 2){ 
       theD.innerHTML += "Status 2: Request recieved ! <br/>"; 
       } 
      else if(xmlHttp.readyState == 3){ 
       theD.innerHTML += "Status 3: Processing Request ! <br/>"; 
       } 
      else if(xmlHttp.readyState == 4){ 

       if(xmlHttp.status == 200){ 
        var text = xmlHttp.responseText; 
        theD.innerHTML += "Status 4: Processing Request ! <br/>"; 
        theD.innerHTML += text; 
        } 
       else{ 
        alert("Something is wrong !"); 
        } 

       } 
} 

的功能,並調用它像

xmlHttp.onreadystatechange = handleServerResponse(); 

它不工作。請指出,如果我錯了。

+0

爲什麼這麼多fuss..Use'$ .ajax'而不是.. –

+1

它改變這一點 - xmlHttp.onreadystatechange = handleServerResponse; –

回答

7

嘗試使用

xmlHttp.onreadystatechange = handleServerResponse;

注意去除paranthesis。

2

兩件事情:

xmlHttp.open("GET", "hello.txt", true); 
xmlHttp.onreadystatechange = handleServerResponse; 
xmlHttp.send(); 

正如你看到的,我刪除了括號,我倒的openonreadystatechange的順序。

第一件事,是因爲否則你不關聯函數引用本身,而是函數的返回值 - 因爲基本上,你正在執行它。這是相同的區別有:

var a = sum(1, 2); // 3, assign to `a` the return value of `sum` 
var b = sum; // assign to `b` the `sum` function ref. 
var c = b(1, 2); // 3, therefore `b` is an 'alias' to `sum` 

第二件事情,這取決於瀏覽器:例如,IE的一些版本「復位」一個XMLHttpRequest實例的onreadystatechange,每次open方法被調用的時間。因此,如果您在open之前設置了之前的那個作爲「初始化程序」的onreadystatechange,則一旦調用open方法,就有機會取決於瀏覽器將被刪除 - 因此從未被調用。 因此要完全兼容,最好在open方法之後設置onreadystatechange - 但當然要在send之前。

1

使用本xmlHttp.onreadystatechange = handleServerResponse

然後寫成函數handleServerResponse(XMLHTTP)它會工作

+0

您能否詳細說明爲什麼這會起作用?我遇到了同樣的問題,但這並沒有解決問題。這將是有用的知道爲什麼這是一個內聯函數 –

+0

handleServerResponse();將是對該函數的調用。 在xmlHttp.onreadystatechange = handleServerResponse的情況下,您正在設置對handleServerResponse的內存地址的引用,以便其他內容可以調用它。 – jradxl