Getjson -无法读取本地json文件

getjson - unable to read a local json file

本文关键字:json 文件 读取 Getjson      更新时间:2023-09-26

使用脚本:

<script type="text/javascript">
    console.log("1");
    $.getJSON( "bares.js", function( json ) {
        console.log("2");
        console.log( "JSON Data: " + json.locations[1].nombre);
    });
    console.log("3");
</script>

我无法访问同一目录下的JSON文件。

bare.js与index.html在同一个远程目录中。Jason是:

{"locations":[
      {"nombre":"name1","lat":"41.6504","long":"-0.879137"}, 
      {"nombre":"name2","lat":"41.6504","long":"-0.879137"}, 
      ....       
        ]
}

console.log("2")不打印

这段代码适合我:

在我的项目JSON/samp.js

{"locations":[
  {"nombre":"name1","lat":"41.6504","long":"-0.879137"}, 
  {"nombre":"name2","lat":"41.6504","long":"-0.879137"}]}
我代码:

<script type="text/javascript">
    $(document).ready(function () {
        console.log("1");
        $.getJSON('JSON/samp.js', function (json) {
            console.log("2");
            console.log("JSON Data: " + json.locations[1].nombre);
        });
        console.log("3");
    });
</script>