2017-07-07 51 views
0

我正在处理数据表中的简单ajax示例,它不工作,我无法解释它。我有一个简单的表如下:Datatables.net 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数据源,看起来像这样(我prettied它一点显示在这里,但该文件没有换行符一个长行)。

{"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 warning: table id=tblAddresses - Requested unknown parameter '0' for row 0, column 0.

我曾与阿贾克斯多次尽管从静态数据文件加载从来没有工作过。我无法在JSON或Javascript中找到错误。

回答

2

您以错误的方式绑定数据。你需要在ajax方法之后绑定列,如下图所示:

$("#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"}] 
     }); 

希望它有帮助!

+1

我知道这是愚蠢的,但我看不到它!谢谢! –