我想在本地测试(file:///
协议(protocol))js,它根据 url 查询参数中的名称动态加载 json 文件。
我正在使用 chrome
file:///Users/eladb/Downloads/map/map_tmp.html?jsonPath=routes_2.json
我试过这个:
var jsonPath = getParameterByName('jsonPath');
$.getJSON(jsonPath, function(json) {
console.log(json); // this will show the info it in firebug console
});
得到这个错误:
jquery.min.js:4 XMLHttpRequest cannot load file:///Users/eladb/Downloads/map/routes_2.json?_=1454238808580. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
有没有快速创建本地服务器(mac、java)仅用于测试的方法?
最佳答案
很简单:您不能在生产环境中使用。这是您无法在客户端计算机上规避的安全功能。
不过,您可以在计算机的浏览器设置中禁用它。在 Chrome 中,使用 --disable-web-security
作为命令行参数启动浏览器。 Firefox 可以使用 about:config -> security.fileuri.strict_origin_policy -> false
禁用它。
这是在 MacOS 上启动 Web 服务器的指南:https://discussions.apple.com/docs/DOC-3083
关于javascript - js如何在使用文件:///protocol时动态加载json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35113192/