我正在研究数据表中的一个简单的ajax示例,它无法正常工作,我无法解释它。我有一个简单的表格如下:
<table id="tblAddresses">
<thead>
<tr>
<th>Street Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Street Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
</tr>
</tfoot>
</table>
我有一个json数据源,其数据看起来像这样(我在这里显示了一点,但文件是一条没有换行的长行)。
{"data":[{"street":"19 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"27 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"31 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"35 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"39 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"49 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"}]}
最后,我将它加载到我的文档就绪函数中:
<script type="text/javascript">
$(document).ready(function(){
$("#tblAddresses").DataTable({
"ajax" : {
"url" : "/json/07055.json",
"columns" : [{"data":"street"},
{"data":"city"},
{"data":"state"},
{"data":"postcode"}]
}
});
});
</script>
当我加载页面时,我看到了ajax调用。我可以看到浏览器接受的数据,但DataTables给了我一个错误:
DataTables警告:table id = tblAddresses - 第0行第0列请求的未知参数“0”。
我曾经多次使用ajax,但从未从静态数据文件加载。我在JSON或Javascript中找不到错误。
答案 0 :(得分:2)
您以错误的方式绑定数据。您需要在ajax方法之后绑定列,例如bellow,
$("#tblAddresses").DataTable({
"ajax" : {
"url" : "/json/07055.json",
"type": "Get"
}, //Here end of ajax method. Now you can bind the columns
"columns" : [{"data":"street"},
{"data":"city"},
{"data":"state"},
{"data":"postcode"}]
});
希望它有所帮助!