Ajax加载时出现Datatables.net错误 [英] Datatables.net error on Ajax load
问题描述
我正在研究数据表中的一个简单的ajax示例,但它不起作用,我无所适从地对其进行解释.我有一个简单的表格,如下所示:
I am working on a simple ajax example in datatables and it is not working and I am at a loss to explain it. I have a simple table as follows:
<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数据源,其数据看起来像这样(我在这里稍作点缀以显示在这里,但是文件是一个长行,没有换行符).
I have a json data source with data that looks like this (I prettied it up a bit for display here but the file is one long line with no line breaks).
{"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"}]}
最后,我将其加载到我的文档就绪功能中:
Finally, I load it in my document ready function:
<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给我一个错误:
When I load the page, I see the ajax call. I can see the data accepted by the browser but DataTables is giving me an error:
DataTables警告:表id = tblAddresses-请求的未知参数'0',用于第0行第0列.
DataTables warning: table id=tblAddresses - Requested unknown parameter '0' for row 0, column 0.
在从未从静态数据文件加载之前,我已经使用ajax多次.我在JSON或Javascript中找不到错误.
I have worked with ajax many times before albeit never loading from a static data file. I can't find the error in the JSON or Javascript.
推荐答案
您以错误的方式绑定数据.您需要在ajax方法之后绑定列,例如波纹管,
You are binding data in wrong way. You need to bind columns after ajax method, like 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"}]
});
希望有帮助!
这篇关于Ajax加载时出现Datatables.net错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!