如何从 ajax url 调用带有 _remap 函数的 Codeigniter 函数?

How to call a Codeigniter function with _remap function from an ajax url?

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

Datatables warning table id=table - 无效JSON response

我发现它与数据ajax url有关table似乎无法找到应该加载的控制器功能。

我在我的控制器上使用了一个 _remap() 函数,这就是为什么我认为它与 url 有冲突并且无法调用我的函数。

这是我的控制器:

<?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数据table代码

 $(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
    },
    ],

});

在这种情况下应该如何解决?谢谢

case $method == 'IS_AJAX':

你的 $method 不是 IS_AJAX 这个 url:

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

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

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

编辑:我之前没有提到这一点,但是开关块会评估您传递给它的表达式,因此无需手动进行比较。示例:

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