我想将JSON数据加载到HTML文件中。 问题是,它不加载json数据,当我添加错误事件时,它会给出错误对象的错误警告。请指导我如何解决此问题。甚至,我找不到任何具体的警报来识别错误。怎么解决这个?有没有办法在使用jquery / ajax时获得实际错误? 下面是我的html文件,它使用html调用json数据:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.7.1.min.js"></script>
<script src="Scripts/jquery-ui-1.8.20.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btn382").click(function () {
/* set no cache */
$.ajaxSetup({ cache: false });
$.getJSON("car-sale.json", function (data) {
var html = [];
alert('result');
});
});
});
</script>
</head>
<body>
<input type="submit" id="btn382" />
</body>
</html>
JSON文件如下:
[{
"Manufacturer": "Toyota",
"Sold": 1200,
"Month": "2012-11"
}, {
"Manufacturer": "Ford",
"Sold": 1100,
"Month": "2012-11"
}, {
"Manufacturer": "BMW",
"Sold": 900,
"Month": "2012-11"
}, {
"Manufacturer": "Benz",
"Sold": 600,
"Month": "2012-11"
}, {
"Manufacturer": "GMC",
"Sold": 500,
"Month": "2012-11"
}, {
"Manufacturer": "HUMMER",
"Sold": 120,
"Month": "2012-11"
}]
答案 0 :(得分:0)
你应该定义这个
<script src="car-sale.json"></script>
我用firefox检查这个,它的工作正常,用firefox
检查$.getJSON('car-sale.json', function (data) {
$.each( data, function( key, val ) {
console.log(val.Manufacturer );
});
});