2017-07-07 15 views
0

私はdatatablesの単純なajaxの例で作業していますが、動作していません。私は(私はここに表示のために少しそれをprettiedが、ファイルが改行なしで一本の長いラインである)このようになりますデータをJSON形式のデータソースを持ってAjaxのロードでDatatables.netエラー

<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> 

次のように私は、単純なテーブルを持っています。

{"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呼び出しを参照してください。私は、ブラウザによって受け入れられたデータが、DataTableのは私にエラーを与えていることがわかります。

DataTables warning: table id=tblAddresses - Requested unknown parameter '0' for row 0, column 0.

私は、静的なデータファイルから読み込むことがないとはいえ前に、Ajaxで何度もしてきました。私は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

私はそれは何か愚かでしたが、私はそれを見ることができませんでした!ありがとう! –

関連する問題