2017-07-07 28 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

我知道這是愚蠢的,但我看不到它!謝謝! –