如何使用ajax url中的_remap函数调用Codeigniter函数?

时间:2016-07-03 14:24:41

标签: javascript php ajax codeigniter-3

我正在处理ajax数据表,当我尝试加载页面时出现错误

数据表警告表id =表 - 无效的JSON响应

我发现它与数据表中的ajax url有关,似乎无法找到应该加载的控制器函数。

我在我的控制器上使用了_remap()功能,这就是为什么我认为它与网址冲突并且我的功能无法调用。

这是我的控制器:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Requests extends CI_Controller {
var $pgToLoad;

public function __construct() {
    parent::__construct();
    #this will start the session
    session_start();

    if(!isset($_SESSION['userId']) || !isset($_SESSION['userLevel']) || !isset($_SESSION['employeeid']) || !isset($_SESSION['firstname']) || !isset($_SESSION['lastname'])) {
        redirect('home', 'location');
    }

    #this will load the model
    $this->load->model('Contents');

    #get last uri segment to determine which content to load
    $continue = true;
    $i = 0;
    do {
        $i++;
        if ($this->uri->segment($i) != "") $this->pgToLoad = $this->uri->segment($i);
        else $continue = false;             
    } while ($continue);        
}

public function index() {   
    $this->load->helper('url'); 
    $this->main();

}   

public function main() {
    #set default content to load 
    $this->pgToLoad = empty($this->pgToLoad) ? "Requests" : $this->pgToLoad;
    $disMsg = "";

    #this will delete the record selected
    if($this->uri->segment(2) == 'leave') { 
        $this->leave();
    }

    #this will logout the user and redirect to the page
    if($this->uri->segment(2) == 'logout') {
        session_destroy();
        redirect('home', 'location');
    }                   

    $data = array ( 'pageTitle' => 'Payroll System | ADMINISTRATION',
                    'disMsg'    => $disMsg,                                             
                    'mainCont'  => $this->mainCont );

    $this->load->view('mainTpl', $data, FALSE);
}



    public function ajax_list()
{
    $list = $this->Contents->get_datatables();
    $data = array();
    $no = $_POST['start'];
    foreach ($list as $leave) {
        $no++;
        $row = array();
        $row[] = $leave->id;
        $row[] = $leave->type;
        $row[] = $leave->startdate;
        $row[] = $leave->enddate;
        $row[] = $leave->duration;
        $row[] = $leave->reason;
        $row[] = $leave->status;      

        //add html for action
        $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person('."'".$leave->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
              <a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person('."'".$leave->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';

              $data[] = $row;


    }

    $output = array(
                    "draw" => $_POST['draw'],
                    "recordsTotal" => $this->Contents->count_all(),
                    "recordsFiltered" => $this->Contents->count_filtered(),
                    "data" => $data,
            );
    //output to json format
    echo json_encode($output);
}

#this will display the form when editing the product
public function leave() {


    $data['employee'] = $this->Contents->exeGetEmpToEdit($_SESSION['userId']);  
        $this->mainCont = $this->load->view('pages/requests/leave', '', TRUE);  

}
 public function _remap () {
    $this->main();
}

Ajax数据表代码

 $(document).ready(function () {

   //datatables
table = $('#table').DataTable({

    "processing": true, //Feature control the processing indicator.
    "serverSide": true, //Feature control DataTables' server-side processing mode.
    "order": [], //Initial no order.

    // Load data for the table's content from an Ajax source
    "ajax": {
        **"url": "<?php echo site_url('requests/leave')?>",
        "type": "POST"
    },

    //Set column definition initialisation properties.
    "columnDefs": [
    {
        "targets": [ -1 ], //last column
        "orderable": false, //set not orderable
    },
    ],

});

在这种情况下,什么应该是解决方案?谢谢

1 个答案:

答案 0 :(得分:1)

case $method == 'IS_AJAX':

您的$方法不是带有此网址的IS_AJAX:

http://localhost/2fb/index.php/redirect 这将带您到没有方法的重定向控制器(将默认为“索引”)。你真的需要:

http://localhost/2fb/index.php/redirect/IS_AJAX ......进入那个案子。您似乎将常量IS_AJAX与请求的方法混淆,您在检查索引时似乎正确使用了该方法(尽管这与默认情况相同,因此它是多余的)。

$ method,或者你在_remap()中指定第一个参数的任何名称,将始终是被调用的路由控制器函数。

编辑:我之前没有提到过这个,但是switch块会评估你传递给它的表达式,所以不需要手动进行比较。例如:

switch ($method) {
    // case $method === 'index':
    case 'index':
        $this->load->view('main');
    break;
}