douchi5822 2017-03-10 17:03
浏览 62

Codeigniter - Ajax调用错误(404)

I have a service running for static pages on CodeIgniter and now I want to make it dynamic using Ajax calls, but the Ajax call always returns as 404 error (defined by the alert on the error section). The index method of the controller is accessible. Only the _get_procs method returns 404.

My Javascript:

$(document).ready(function(){
    base_url = '<?= base_url() ?>';
    $('#btnAjax').click(function(){
        alert("AJAX");
        $.ajax({
            url: base_url + 'general-data/_get_procs',
            type: 'POST',
            data: {'period': '1'},
            dataType: 'json'
        }).success(function(response){
            alert(response);
        }).error(function(e){
            alert("Error");
        });
    });
});

My Controller:

function _get_procs(){
    $period = $this->input->post('period');
    echo json_encode("OK");
}
  • 写回答

1条回答 默认 最新

  • doujianwan7570 2017-03-10 21:11
    关注

    Comments from @Dimi showed me what was going on: the use of underscores on the start of functions' names, e.g. _function_one doesn't work; function_one does work, makes CodeIgniter break.

    The solution I came up with was to rename the function to the format function_one (get_procs, on my case) and create a rule on the routes.php config file:

    $route['controller/_get_procs'] = 'controller/get_procs';
    

    This is a workaround to make it work without changing the default configuration of CodeIgniter. I don't know if there is another way.

    As pointed out by @cssBlaster21895, CodeIgniter follows up PHP Coding Style (see more here), which determines _function as a private function.

    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来