javascript - js如何在使用文件:///protocol时动态加载json文件

标签 javascript jquery json url

我想在本地测试(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/

相关文章:

javascript - jqplot y 轴文本高度有剪辑

javascript - hide() 在 jquery mobile 中不能完全工作

javascript - 使用 JavaScript 迭代 JSON 数组中的对象

php改变多维数组的值

javascript - 神奇地改变时间戳

javascript - 访问变量

javascript - JSFiddle 包装在 onLoad 中?

c# - 在 .NET 中的 JSON 响应中嵌入外键对象

javascript - 将正则表达式传递到方法 : 时遇到问题

javascript - jQuery递增类名和函数