2011-12-06 32 views
2

外面我的JavaScript代碼:JavaScript數組空函數

var buffer=new Array(); 

function fetchData(min,max){ 
    var ajaxReq = new XMLHttpRequest(); 
    ajaxReq.onreadystatechange = function(){ 
    if (ajaxReq.readyState === 4) { 
     if (ajaxReq.status === 200) { 
      buffer= ajaxReq.responseText; 
      console.log(buffer)//this logs an array to console 
     } else { 
      console.log("Error", ajaxReq.statusText); 
     } 
    } 
    }; 
    ajaxReq.open('GET', "server/controller.php?min="+min+"&max="+max, true); 
    ajaxReq.send(); 
} 

fetchData(1,100); 
console.log(buffer);//this log an empty array 

兩個日誌有不同的結果,我究竟做錯了什麼?感謝指針。

+0

'fetchData'函數中的'buffer'變量是條件集。你確定'ajaxReq.status'和'ajaxReq.readyState'等於你想要的嗎? –

回答

8

Ajax是異步的。這意味着最後的console.log(buffer)在來自Ajax請求的響應之前執行。

你應該你的方法改成這樣:

function fetchData(min,max,callback){ 
    var ajaxReq = new XMLHttpRequest(); 
    ajaxReq.onreadystatechange = function(){ 
    if (ajaxReq.readyState === 4) { 
     if (ajaxReq.status === 200) { 
     buffer= ajaxReq.responseText; 
     callback(); 
     //console.log(buffer)//this logs an array to console 
     } else { 
     console.log("Error", ajaxReq.statusText); 
     } 
    } 
    }; 
    ajaxReq.open('GET', "server/controller.php?min="+min+"&max="+max, true); 
    ajaxReq.send(); 
} 

fetchData(1,100,function(){ 
    console.log("My Ajax request has successfully returned."); 
    console.log(buffer); 
}); 
+0

優秀的點,錯過了。 –

+0

是的,,,它工作時,我改變爲真。 – bingjie2680

+1

是的,但是您的用戶必須等待Ajax響應,從而鎖定您的頁面。即使現在的反應看起來很快,也不可避免地會出現這種情況。總之,不要讓你的Ajax請求同步。 –

0

似乎我的權利。緩衝區是空的以便啓動,並且在異步調用完成之後它纔會被設置,所以即使您在第二個console.log之前獲取數據,直到它顯示空緩衝區之後纔會收到它。

2

這是異步的。所以,你的流程是這樣的:

  1. 呼叫fetchData()
  2. AJAX請求被髮送,註冊一個onreadystatechange回調
  3. fetchData()完成並返回
  4. buffer被註銷,這還不包含任何東西。
  5. 稍後,ajax請求完成並觸發回調
  6. 回調函數將數組放入數組中。
  7. buffer get從回調中註銷,並且您看到它現在包含項目。

因此,只有當您點擊第一個console.log時,纔會啓動異步請求。但事實上,這件事實際上很久之後就結束

1

這裏有幾個問題。當ajax調用完成時,第二個console.log已經在變量設置之前執行。

另外,您沒有使用buffer作爲Array變量。

+0

你是什麼意思不使用緩衝區作爲數組,我怎樣才能使用它作爲一個數組,謝謝你的幫助。 – bingjie2680

+0

'buffer.push(ajaxReq.responseText);''將響應文本推到數組末尾 – Trevor

+0

很好,謝謝... – bingjie2680

0

MDN:https://developer.mozilla.org/en/XMLHttpRequest

void open(in AUTF8String method, in AUTF8String url, in boolean async, in AString user, in AString password); 

第三個參數是用來告訴瀏覽器的請求是否應作出異步與否。你將它設置爲true,因此它將是異步的。 異步基本上意味着請求被髮送,同時其他代碼被執行。因此,它開始請求,並在等待響應時,它會在之前記錄緩衝區:請求已完成。如果要記錄內容,請在onreadystatechange事件中執行此操作,或者將第三個參數(異步)設置爲false。

3

您正試圖在執行AJAX請求之前緩衝log()。爲了解決這個問題,你的fetchData函數需要處理callback函數。

var buffer=new Array(); 

function fetchData(min,max, callback){ 
    var ajaxReq = new XMLHttpRequest(); 
    ajaxReq.onreadystatechange = function(){ 
    if (ajaxReq.readyState === 4) { 
     if (ajaxReq.status === 200) { 
      buffer= ajaxReq.responseText; 
      console.log(buffer)//this logs an array to console 
      if(typeof callback == 'function'){ 
       callback.call(this); 
      } 
     } else { 
      console.log("Error", ajaxReq.statusText); 
     } 
    } 
    }; 
    ajaxReq.open('GET', "server/controller.php?min="+min+"&max="+max, true); 
    ajaxReq.send(); 
} 

fetchData(1,100, function(){ 
    console.log(buffer); 
}); 

這是最基本的實現,只有當AJAX響應成功時才能工作。