2017-07-06 45 views
-1

所以我有一个像下面加载JSON转换为JavaScript

www.pretendJsonLink.com/getData 

和我收到的数据的JSON的HTML链接如下:

{"Items":[{"date":1498850140373,"displayId":"003","sentId":"121213", 
"customer":"ME"}, {"date":1452870140571,"displayId":"007","sentId":"152713", 
"customer":"YOU"}],"Count":2,"ScannedCount":2} 

我需要这个加载到一个js文件,所以我然后可以给他们打电话需要在一个id一个HTML文件=“”标签

像Items.date和Items.customer或类似的东西

任何帮助将是伟大的,我明白,这应该是一个简单的任务,但我也可以转发我的搜索历史,以及:)我一直在寻找解决方案的工作,但只是似乎找不到任何符合我的需求

+2

JSON.parse https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON /解析 – wostex

+0

你的代码是什么样的?你使用的是XMLHttpRequest(又名AJAX)吗?如果是这样,为什么不在回调中设置一个变量,然后用它做什么? –

+1

你能解释一下你这个问题的一部分吗,“我收到的数据是低于”?你好吗?您目前需要接收的代码是什么? – Adam

回答

-1

您可以使用XMLHttpRequest。我发现了一个gist,给出了一个很好的例子,其中包括它(简化的),如下:

var req = new XMLHttpRequest(); 
req.open('GET', 'www.pretendJsonLink.com/getData'); 

req.onload = function() { 
    if (req.status == 200) { 
     // do what you want, here, with the req.response 
     // take a look at the object that gets returned, you may need 
     // to call JSON.parse(), etc. 
     console.log('success', req.response); 
    } else { 
     console.log('error', req.statusText); 
    } 
}; 

// Handle network errors 
req.onerror = function() { 
    console.log("Network Error"); 
}; 

// Make the request 
req.send();