Javascript getjson-无法读取本地json文件
使用此脚本:Javascript getjson-无法读取本地json文件,javascript,jquery,getjson,Javascript,Jquery,Getjson,使用此脚本: <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>
<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>
console.log(“2”)
未打印。此代码适用于我:
在我的项目JSON/samp.js中
我的代码:
<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>
$(文档).ready(函数(){
控制台日志(“1”);
$.getJSON('JSON/samp.js',函数(JSON){
控制台日志(“2”);
log(“JSON数据:+JSON.locations[1].nombre”);
});
控制台日志(“3”);
});
浏览器开发人员工具中的“网络”选项卡显示了什么?控制台是否报告任何错误?定义“本地”<代码>http://localhost/或file://blah 废话
<代码>重要提示:从jQuery1.4开始,如果JSON文件包含语法错误,请求通常会自动失败还测试来自文件的ajax请求://(例如,从桌面在浏览器中打开html文档)不会起作用。你确定bares.js
包含JSON而不是像文件结尾所示的JavaScript吗?如@FelixKling所说,看起来您使用了错误的文件扩展名:.js
而不是.json
。那么您是说OP的代码没有问题?那么写答案有什么意义呢?应该是一个评论。
{"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>