繁体   English   中英

如何从ajax网址中使用_remap函数调用Codeigniter函数?

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

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

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

我发现它与datatable中的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':

您的$方法不是使用此URL的IS_AJAX:

http://localhost/2fb/index.php/redirect这将带您进入没有方法的重定向控制器(默认为“ index”)。 您实际上需要:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM