如何使用_remap函数从ajax url调用Codeigniter函数


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

我正在研究ajax数据表,当我试图加载页面时,我得到了一个错误

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

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

我在我的控制器上使用_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数据表代码

 $(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':

你的$方法不是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;
}