【问题标题】:could not get data from file by ajax with jquery datatable无法通过 ajax 使用 jquery 数据表从文件中获取数据
【发布时间】:2015-09-13 01:23:49
【问题描述】:

我在这里使用示例:https://datatables.net/examples/styling/jqueryUI.html

下面是我的html和jquery代码

<html>
<head>
  <link rel="stylesheet" type="text/css" href="jquery-dataTables.css">
  
  <script type="text/javascript" charset="utf8" src="jquery-min.js"></script>
  <script type="text/javascript" charset="utf8" src="jquery-dataTables.js"></script>
  <script>
  
  $(document).ready(function() {
    $('#example').dataTable( {
        "ajax": "data/test.txt",
        "columns": [
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "extn" },
            { "data": "start_date" },
            { "data": "salary" }
        ]
    } );
} );
  </script>
  
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
 
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>
</body>
</html>

这是我文件的结构

但它无法将数据加载到数据表中,有人可以帮忙吗?

编辑(附上我的测试数据):此数据用于

“数据”:[ { “名称”:“老虎尼克松”, "position": "系统架构师", “薪水”:“320,800 美元”, “开始日期”:“2011/04/25”, “办公室”:“爱丁堡”, “分机”:“5421” }, { “名称”:“加勒特温特斯”, “职位”:“会计师”, “薪水”:“170,750 美元”, “开始日期”:“2011/07/25”, “办公室”:“东京”, “分机”:“8422” }, { “名称”:“阿什顿考克斯”, "position": "初级技术作者", “薪水”:“86,000 美元”, “开始日期”:“2009/01/12”, “办公室”:“旧金山”, “分机”:“1562” }, { “名称”:“塞德里克·凯利”, "position": "高级 Javascript 开发人员", “薪水”:“433,060 美元”, “开始日期”:“2012/03/29”, “办公室”:“爱丁堡”, “分机”:“6224” },
{ “名称”:“卡拉·史蒂文斯”, “职位”:“销售助理”, “薪水”:“145,600 美元”, “开始日期”:“2011/12/06”, “办公室”:“纽约”, “分机”:“3990” },
{ “名称”:“唐娜·斯奈德”, “职位”:“客户支持”, “薪水”:“112,000 美元”, “开始日期”:“2011/01/25”, “办公室”:“纽约”, “分机”:“4226” } ]

谢谢

【问题讨论】:

  • test.txt的内容怎么样?

标签: jquery ajax datatables


【解决方案1】:

我假设您使用了来自Datatables ajax simple example 的示例test.txt 文件,但在this example 中定义了列。问题是test.txt 文件没有列,您的数据表需要。所以有两种方法可以修复它: 不要在数据表中定义列

$(document).ready(function() {
    $('#example').dataTable( {
        "ajax": "data/test.txt"
    } );
} );

或更改您的 test.txt 文件以匹配列,例如:

"data": [
{
  "name": "Airi",
  "position": "Accountant",
  "office": "Tokyo",
  "extn": "...",
  "start_date": "28th Nov 08",
  "salary": "$162,700"
},...

【讨论】:

  • 感谢您的回复,但两种解决方案均无效。 :(
  • 能否提供test.txt的内容,这样比较方便
最近更新 更多