jQuery $ .getJSON加载本地JSON文件不起作用 [英] JQuery $.getJSON load local JSON file not working
本文介绍了jQuery $ .getJSON加载本地JSON文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题很简单,我有一个名为new.json的文件,并且试图使用JQuery加载和显示数据.我一直在写JavaScript代码:
My problem is very simple, I have a file with the name of new.json and I am trying to use JQuery to load and display the data. I have been writing JavaScript code:
$.getJSON("new.json", function(data){
// I have placed alert here previously and realized it doesn't go into here
$.each(data.streetCity, function(i,s){
alert(s);
});
});
}
,new.json中的数据如下:
and the data in new.json looks as below:
{"streetCity":
{
"1":"Abergement-Clemenciat",
"2":"Abergement-de-Varey",
"3":"Amareins"
}
};
推荐答案
如果您使用的是Chrome.因为Chrome不允许xmlhttprequest请求本地文件.因此,jQuery无法加载您的new.json
If you are using Chrome. Because Chrome don't allow xmlhttprequest to request local file. So jquery can not load your new.json
您可以添加
-允许文件访问文件
进入chrome的启动命令.这可以允许xmlhttprequest加载本地资源
to the start command of chrome. This can allow xmlhttprequest to load local resource
这篇关于jQuery $ .getJSON加载本地JSON文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文