2017-10-20 98 views
0

我有一個這樣的變量文件。如何獲取JSON數據並存儲到變量中。

var geography = [ 
        { id:"Country", header:"", width:150}, 
        { id:"Capital", header:"Capital", width:150}, 
        { id:"Falg", header:"Falg", width:150}, 
        { id:"Language", header:"Language", width:150}, 
        {id:"Population", header:"Population", width:150}, 
       ], 

現在我想從json加載這些數據。我將這些數據放入JSON文件並使用此代碼。

getGeography function(){ 
     var geography; 
     var xmlhttp = new XMLHttpRequest(); 
     xmlhttp.open("GET", "data.json",true); 
    } 

現在從這裏如何存儲到一個變量並返回。

+0

使用[onreadystatechange的(https://開頭developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange)ie'xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState === XMLHttpRequest.DONE && xmlhttp.status === 200)geography = xmlhttp.responseText; } };' – Satpal

回答

3

當xmlhttp的就緒狀態爲4且響應狀態爲200時,應該讀取它。您應該使用JSON.parse()解析響應。但是,您不能從函數返回值。因爲默認情況下XMLHTTPRequest是異步的。

function getGeography() { 
var geography; 
var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("GET", "data.json",true); 
xmlhttp.onreadystatechange = function() { 
    if(xmlhttp.readyState === 4 && xmlhttp.status === 200) { 
    geography = JSON.parse(xmlhttp.responseText;) 
    } 
} 
} 

而不是返回地理,你必須以編程方式讀取AJAX請求完成時的地理價值。像這樣的東西(read this):

而不是

寫這樣的代碼:

function anotherFunc() { 
var geography = getGeography(); 
thenDoSomething(geography); 
} 

這樣寫:

function anotherFunc() { 
getGeography(); 
} 
function getGeography() { 
var geography; 
var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("GET", "data.json",true); 
xmlhttp.onreadystatechange = function() { 
    if(xmlhttp.readyState === 4 && xmlhttp.status === 200) { 
    geography = JSON.parse(xmlhttp.responseText); 
    thenDoSomething(geography); 
    } 
} 
} 

這就像交出代碼的其餘部分的執行控制getGeography()函數,而不是期待函數的返回值,然後使用該值。當AJAX調用完成時,getGeography()函數使用從AJAX響應接收到的值繼續執行其餘代碼。

+0

我需要將'geography'聲明爲數組嗎? – David

+2

不,JavaScript不需要定義類型。 'var geography';'你聲明變量並賦值'未定義的值。你可以分配任何你想要的,然後分配其他的東西。該類型將自動更改。 –

+0

在這種情況下,我也gettong 狀態變得0而不是200 – David

1

我不是jQuery的粉絲,但在這種情況下,你可能會從中受益。

$.get("ajax/test.html", function(data) { 
    // data is your result 
    console.log(data); 
    console.log(JSON.parse(data)); 
}); 

https://api.jquery.com/jquery.get/

0

下面是如何使用XMLHttRequest():

<script> 
    const req = new XMLHttpRequest(); 
    var geography = []; 
    req.onreadystatechange = function(event) { 
     // XMLHttpRequest.DONE === 4 
     if (this.readyState === XMLHttpRequest.DONE) { 
      if (this.status === 200) { 
       geography = JSON.parse(this.responseText); 
       console.log(geography); 
       alert("Great Success : check console !"); 
      } else { 
       alert("Something has gone really Bad !"); 
      } 
     } 
    }; 

    req.open('GET', 'data.json', true); 
    req.send(null); 

謹慎使用JSON正確:

[ 
    {"id":"Country","header":"","width":150}, 
    { "id":"Capital","header":"Capital", "width":150}, 
    { "id":"Falg","header":"Falg","width":150}, 
    { "id":"Language","header":"Language", "width":150}, 
    { "id":"Population", "header":"Population", "width":150} 
] 
+0

狀態變爲0而不是200 – David

+0

您使用一個Web服務器?你不應該在本地打開你的html文件,我的意思是通過文件協議。使用網絡服務器。例如,您可以使用「用於Chrome的Web服務器」,它是一個Chrome插件。我用它測試了我的例子,它工作正常:-) – PhilMaGeo

相關問題