2016-10-05 14 views
0

背景:我試圖檢索數據,使用AJAX,並將其分配給一個變量。我試圖將AJAX請求設置爲同步,但Firefox不會允許它。AJAX - 我如何確定收到所有數據?

問題:如何確定收到的所有數據?

function search(){ 
    var data = []; 
    this.init = function(){ 
     data = getData({"url":"/imglib/Inventory/cache/2335/VehInv.js"}); 
     console.log(data); // Returns as 'undefined'. Possibly because of asynchronous call? 
    }; 
    var d = new Date(); 
    function getData(url){ 
     var xhttp: new XMLHttpRequest(); 
     var dataURL = url + '?v=' String(d.getTime()); 
     xhttp.onreadystatechange = function(){ 
      if(this.readyState = 4 && this.status == 200){ 
       var r = this.responseText; 
       var s = r.indexOf('[') + 1; 
       var e = r.indexOf(']'); 
       var jsonData = JSON.parse("[" + r.slice(s,e) + "]"); 
       return jsonData; 
      } 
     }; 
     xhttp.open("GET", dataURL, true); 
     xhttp.send(); 
    } 
}; 
+0

聽起來像是你可能想看看承諾:http://bluebirdjs.com/docs/getting-started.html – mherzig

+0

@mherzig謝謝。我真的很感激這個意見。不幸的是,我將要部署這個環境需要使用純JavaScript。 – H0nd0

+0

或者在'ondreadystatechange'函數中移動'console.log(data)',並執行'console.log(jsonData)'。您之所以未定義它,是因爲AJAX本質是異步的。從'XMLHttpRequest'文檔中'async'的默認值是真的......你是否試圖將它設置爲'false'? 'xhttp.open('GET',dataUrl,false)' –

回答

0

你有異步東西工作時使用回調..

function search(){ 
    this.init = function(){ 
     getData("http://www.petesrvvt.com/imglib/Inventory/cache/2335/VehInv.js", function(data){ 
      console.log(data); // Returns as 'undefined'. Possibly because of asynchronous call? 
     }); 
    }; 
    var d = new Date(); 
    function getData(url, callback){ 
     var xhttp: new XMLHttpRequest(); 
     var dataURL = url + '?v=' String(d.getTime()); 
     xhttp.onreadystatechange = function(){ 
      if(this.readyState = 4 && this.status == 200){ 
       var r = this.responseText; 
       var s = r.indexOf('[') + 1; 
       var e = r.indexOf(']'); 
       var jsonData = JSON.parse("[" + r.slice(s,e) + "]"); 
       callback(jsonData); 
      } 
     }; 
     xhttp.open("GET", dataURL, true); 
     xhttp.send(); 
    } 
}; 
+0

謝謝!當我回家時,我會研究這一點。 – H0nd0

0

通話將處理來自調用的「的onreadystatechange」功能數據的功能。它不起作用的原因是變量「data」在您嘗試使用它時未使用異步查詢的結果進行定義。

(function search() { 
 
    var data = []; 
 
    this.init = function() { 
 
    data = getData({ 
 
     "url": "http://www.petesrvvt.com/imglib/Inventory/cache/2335/VehInv.js" 
 
    }); 
 
    // This is definitely caused by the asynchronous XMLHttpRequest 
 
    //console.log(data); // This needs to be moved to the callback that is invoked when the request completes. See below 
 
    }; 
 
    var d = new Date(); 
 

 
    function getData(url) { 
 
    var xhttp: new XMLHttpRequest(); 
 
    var dataURL = url + '?v=' 
 
    String(d.getTime()); 
 
    xhttp.onreadystatechange = function() { 
 
     if (this.readyState = 4 && this.status == 200) { 
 
     var r = this.responseText; 
 
     var s = r.indexOf('[') + 1; 
 
     var e = r.indexOf(']'); 
 
     var jsonData = JSON.parse("[" + r.slice(s, e) + "]"); 
 
     // This is how you be sure you get your data 
 
     console.log(jsonData); 
 
     } 
 
    }; 
 
    xhttp.open("GET", dataURL, true); 
 
    xhttp.send(); 
 
    } 
 
})();