使用codeigniter中的ajax源将数据提取到datatable返回空

时间:2015-06-26 03:52:49

标签: php jquery ajax json codeigniter

我正在尝试使用codeigniter框架中的ajax源将我的查询结果传递给datatable我在下面尝试了这个代码,但是我的数据表返回空的im new这个方法就是我做的:

控制器:

public function for_source() {
        $query = mysql_query("SELECT user_fname, user_lname FROM user_account WHERE user_priviledge = 'student' LIMIT 20");

        $data = array();

        while($row=mysql_fetch_array($query)) {
        $data['data'][] = array(
                            'fname' => $row['user_fname'],
                            'lname' => $row['user_lname'],
                            );
        }
       echo json_encode($data);
}

我的观点:

<table id="getWiwit" class="table table-border table-hover table-striped">
 <thead>
    <tr>
        <td><div style="margin-left: -7px;">user name</div></td>
        <td><div style="margin-left: -7px;">user lname</div></td>
    </tr></thead></table>

以下是我尝试使用ajax源将数据放入datatable的方法

$(document).ready(function() {
console.log(baseUrl+"dashboard.php/acceptedtransaction/for_source/");
var table = $('#getWiwit').DataTable({
    "ajax": {
        "url": baseUrl+"dashboard.php/acceptedtransaction/for_source/",
        "type": "POST"
        },
    "columns": [
        {
            "class":          'details-control',
            "orderable":      false,
            "data":           null,
            "defaultContent": ''
        },
        { "data": "fname"},
        { "data": "lname"},
    ],
    "order": [[1, 'asc']]
});
});

请帮助

2 个答案:

答案 0 :(得分:0)

您是否尝试将您的网址baseUrl+"dashboard.php/acceptedtransaction/for_source/"放入浏览器并查看ajax返回的内容?

答案 1 :(得分:0)

  "ajax": {
    "url": "<?php echo base_url();?>index.php/acceptedtransaction/for_source/",
    "type": "POST"
    },

此网址应为

"<?php echo base_url();?>index.php/acceptedtransaction/for_source/",

base_url()/index.php/controller(name)/method(name)