【发布时间】:2015-02-06 23:40:58
【问题描述】:
所以我在这里关注http://datatables.net/examples/ajax/simple.html 的信息,只是为了设置一个非常基本的数据表。一旦完成,我将努力按照我想要的方式完成它。这是我的代码:
表:
<table id='transactions' class='table table-hover'>
<thead>
<tr>
<th>Symbol</th>
<th>Company Name</th>
<th>Market</th>
<th>Purchase Price</th>
<th>Commission</th>
<th>Shares</th>
<th>Purchase Date</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Symbol</th>
<th>Company Name</th>
<th>Market</th>
<th>Purchase Price</th>
<th>Commission</th>
<th>Shares</th>
<th>Purchase Date</th>
</tr>
</tfoot>
</table>
Javascript:
$('table#transactions').dataTable({
ajax: url
});
数据(收到):
{"data":
[
["AAPL","Apple Inc.","NYSE","30.00","0.00","100","2015-02-03"],
["T.TO","TELUS Corporation","TSE","12.75","1.25","400","2015-02-03"],
["SNE","Sony Corporation","NASDAQ","42.63","2.50","50","2015-01-28"]
]
}
所以问题是我在标题中遇到了错误,我不知道为什么。据我所知,我几乎完全复制了示例页面上的代码,并复制了数据的发送方式。我希望另一双眼睛能帮助我告诉我哪里出错了。
编辑:
我已将要发送的数据更改为尽可能少的数据,并检查了网络的实际响应,这就是我所看到的:
"{\"data\":[\"AAPL\",\"Apple Inc.\",\"NYSE\",\"30.00\",\"0.00\",\"100\",\"2015-02-03\"]}"
该数据的输出是否让我感到困惑?我应该提到我正在通过 PHP 发送这些数据,它是一个我使用 json_encode 的数组。
【问题讨论】:
-
“得到标题中的错误”是什么意思。您遇到什么错误?
-
@minion 错误信息是“cannot read property 'length' of undefined”,在问题的标题中。
-
确保您的 Javascript 在
$(document).ready()函数中。 -
是的,对不起,我没有把它包括在内,我认为没有必要。
-
这个错误到底发生在哪一行?它是否发生在数据表库中?您还检查了
url的定义是否正确?库是否正确包含(首先包含 jQuery)?