2017-02-10 43 views
-1

我已經經歷了這裏所有的其他的答案看了,但找不到一個簡單地陳述一個解決方案,我有錯誤,數據表請求的未知參數

我的JS代碼:

$('#product-table').DataTable({ 
    stateSave: true, 
    ajax: { 
     url: "/api/stock/products", 
     type: "POST", 
     "Columns": [ 
      { "data": "name" }, 
      { "data": "description" }, 
      { "data": "current_stock" }, 
      { "data": "cost_price" }, 
      { "data": "retail_price" } 
     ] 
    }, 
}); 

JSON數據返回:

{ 
    "data": [ 
    { 
     "name": "product 1", 
     "description": "test product", 
     "current_stock": "200", 
     "cost_price": "2000", 
     "retail_price": "2500" 
    } 
    ] 
} 

HTML:

<table id="product-table" width="100%" class="table table-hover"> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>Description</th> 
       <th>Current Stock</th> 
       <th>Cost Price</th> 
       <th>Retail price</th> 
      </tr> 
     </thead> 
</table> 

AJAX調用正在通過並返回數據發送,但只要在頁面加載的DataTable引發以下錯誤:

DataTables warning: table id=product-table - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

回答

1

那JS不看的權利,試試這個來代替:

$('#product-table').DataTable({ 
    "stateSave": true, 
    "ajax": { 
     "url": "/api/stock/products", 
     "type": "POST" 
    }, 
    "columns": [{ 
     "data": "name" 
    }, { 
     "data": "description" 
    }, { 
     "data": "current_stock" 
    }, { 
     "data": "cost_price" 
    }, { 
     "data": "retail_price" 
    }] 
}); 
相關問題