代码改变世界

AngularJS $http.get 本地json 失败

2017-09-22 03:12  Essense&&Root  阅读(697)  评论(0)    收藏  举报

问题详情:

json数据硬编码(hardcoded)在程序中时,正确显示;但是把数据存入本地json时,在chrome中没反应,没有error,只是没有数据显示。

google search: angularjs $http.get local json failed stackoverflow

(大家一定要使用 google+英文,如果是百度,估计永远找不到答案;不纯是百度的问题,而是中文世界此部分知识就是不完整不健全的)

找到了答案:许多浏览器不允许本地文件去访问其他本地文件。(对于我这个情境,chrome是不允许的;但是我用FireFox是可以的。tmd,惊了)

1) If you're not running a webserver of any kind and just testing with file://index.html, then you're probably running into same-origin policy issues. See:

https://code.google.com/archive/p/browsersec/wikis/Part2.wiki#Same-origin_policy

Many browsers don't allow locally hosted files to access other locally hosted files. Firefox does allow it, but only if the file you're loading is contained in the same folder as the html file (or a subfolder).

 

详情见:

https://stackoverflow.com/questions/16930473/angularjs-factory-http-get-json-file