使用此脚本:
<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的同一个远程目录中。杰森是:
{"locations":[
{"nombre":"name1","lat":"41.6504","long":"-0.879137"},
{"nombre":"name2","lat":"41.6504","long":"-0.879137"},
....
]
}
console.log("2")
未打印。
答案 0 :(得分:0)
此代码适用于我:
在我的项目 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>