admin管理员组文章数量:1416651
I had to read a json file by javascript during an interview.
The json file is almost like this:
{ apple: {price: 1}, banana: {price: 2} }
I have got some solutions like:
- read it by the help of ajax method
- modify json file like "var json= { apple: {price: 1}, banana: {price: 2} }" and load it into HTML just like a javascript file, so I can read it as a global variable
However when I asked the Interviewer, he gave me the hints:
load json file to html using script tag like this:
script type="application/json" src="scripts/data.json"
then read the data in your js file by
eval(json)
I was confused: how could I access the data just by loading it as script tags without modifing?
I had to read a json file by javascript during an interview.
The json file is almost like this:
{ apple: {price: 1}, banana: {price: 2} }
I have got some solutions like:
- read it by the help of ajax method
- modify json file like "var json= { apple: {price: 1}, banana: {price: 2} }" and load it into HTML just like a javascript file, so I can read it as a global variable
However when I asked the Interviewer, he gave me the hints:
load json file to html using script tag like this:
script type="application/json" src="scripts/data.json"
then read the data in your js file by
eval(json)
I was confused: how could I access the data just by loading it as script tags without modifing?
Share Improve this question asked Dec 5, 2015 at 22:03 Black JohnBlack John 311 silver badge2 bronze badges 1- That isn't JSON. Run it through jsonlint. – Quentin Commented Dec 5, 2015 at 23:23
3 Answers
Reset to default 3You can't do it this way:
HTML/Javascript: how to access JSON data loaded in a script tag with src set
Could this be a trick question from the interviewer..?
Use ajax, that is the only proper way to do it in my opinion. Other solutions fall into the hack-category.
Also, as mentioned by Franco, don't use eval(), unless you have to support very old browsers and don't care about security. Use JSON.parse() instead. It's even supported by IE8. Calling eval evaluates/executes its argument - so this is an attack vector for someone trying to inject malicious code into your site.
You may have a bad recollection of the details of the questions.
Obviously, the first part of the question involves Ajax (XmlHttpRequest).
The second part is probably related to JSONP, where you modify the server output so that it looks more like function(<json data here>)
, with the function name being provided by the client to the server (though that's really implementation-specific). JSONP used to be a mon pattern a few years back (mostly as a backwards patibility solution for browsers without XHR support, but also to bypass cross-domain limitations). Probably a lot less these days, as it has a few significant security issues. If you want to read more about it: https://en.wikipedia/wiki/JSONP
JSONP does not, however, involve the use of eval
, but has the same kind of security issues: anything goes.
You will need to get the json file (this suppose you want to get it by a button click):
$(document).ready(function(){
$("button").click(function(){
$.getJSON("your-json.js", function(result){
$.each(result, function(i, val){
//you can do something with your returned data.
});
});
});
});
NOTE: don't use eval(), this is a bad practice.
本文标签: Try to load json file directly into HTML and read its contents by javascriptStack Overflow
版权声明:本文标题:Try to load json file directly into HTML and read its contents by javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745255205a2650058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论