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

标签 javascript php ajax codeigniter-3

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

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

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

我在 Controller 上使用 _remap() 函数,这就是为什么我认为它与网址冲突并且无法调用我的函数。

这是我的 Controller :

<?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':

您的 $method 不是带有以下网址的 IS_AJAX:

http://localhost/2fb/index.php/redirect 这会将您带到没有方法的重定向 Controller (默认为“index”)。你确实需要:

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

$method,或者您在 _remap() 中命名的第一个参数,将始终是被调用的路由 Controller 函数。

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

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

关于javascript - 如何从 ajax url 调用带有 _remap 函数的 Codeigniter 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170890/

相关文章:

php - 从 UI 将 API key 存储在数据库中

javascript - JQuery Ajax提交功能添加验证

jquery - $(document).on ('click' ... $.ajax 调用仅执行一次

php - mysql 结合随机排序()和按值排序——可能吗?

php - 正确寻址 "Undefined property: stdClass::"

javascript - 关于PHP、MYSQL、AJAX的问题

javascript - requestAnimFrame 在 Chrome 中为 60FPS,在 FF 中为 1FPS

javascript - 使用 bootstrap 模式调用 iframe 中的页面?

javascript - 使用 Jest 和 React 模拟非默认函数

javascript - 如何检索先前属性的名称属性