admin管理员组文章数量:1331849
My json file looks like this
{
"Persons": {
"Name" : "e",
"Name2": "e",
"Id": "4700"
}, [...]
}
How does my code looks like to parse/load this local json file into a html file. I tried everything out but none of them worked.
My json file looks like this
{
"Persons": {
"Name" : "e",
"Name2": "e",
"Id": "4700"
}, [...]
}
How does my code looks like to parse/load this local json file into a html file. I tried everything out but none of them worked.
Share Improve this question edited Jan 21, 2015 at 19:52 Sean Kendle 3,6291 gold badge29 silver badges35 bronze badges asked Dec 8, 2014 at 14:35 m1711m1711 1111 silver badge8 bronze badges 17-
1
You need to make an AJAX GET request to load data. Then you would use
JSON.parse
to parse text loaded content into object data. – dfsq Commented Dec 8, 2014 at 14:38 - What are you using to retrieve the data? An attempt should help everyone out. – Mr. Polywhirl Commented Dec 8, 2014 at 14:39
- @dfsq AJAX GET of the local file will fails in Chrome. – lexicore Commented Dec 8, 2014 at 14:40
- @lexicore it should work if running a local webserver. – brbcoding Commented Dec 8, 2014 at 14:41
- 2 @brbcoding I'd argue that "load a resource using a local webserver" is not the same as "load a local file". – lexicore Commented Dec 8, 2014 at 14:42
1 Answer
Reset to default 6Here's an example from (http://youmightnotneedjquery./)
request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400){
// Success!
data = JSON.parse(request.responseText);
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
Your data
variable will then have accessible members like this:
alert(data.Persons.Name);
本文标签: ajaxHow do i load my local json file just using JavaScriptStack Overflow
版权声明:本文标题:ajax - How do i load my local json file just using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742225524a2436242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论