JQuery $ .getJSON加载本地JSON文件不起作用

yi2*_*ng2 3 jquery json getjson

我的问题非常简单,我有一个名为new.json的文件,我正在尝试使用JQuery来加载和显示数据.我一直在编写JavaScript代码:

$.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);
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

new.json中的数据如下所示:

{"streetCity":
    {
        "1":"Abergement-Clemenciat",
        "2":"Abergement-de-Varey",
        "3":"Amareins"
    }
};
Run Code Online (Sandbox Code Playgroud)

小智 11

如果您使用的是Chrome.因为Chrome不允许xmlhttprequest请求本地文件.所以jquery无法加载你的new.json

你可以加

--allow-文件访问从 - 文件

到chrome的启动命令.这可以允许xmlhttprequest加载本地资源