2017-10-20 106 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

相关问题