Codeigniter-Ajax调用时出错(404) [英] Codeigniter - Error on Ajax call (404)

查看:53
本文介绍了Codeigniter-Ajax调用时出错(404)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CodeIgniter上为静态页面运行了一个服务,现在我想使用Ajax调用使其动态,但是Ajax调用始终返回404错误(由错误部分的警报定义).可以访问控制器的索引方法.只有_get_procs方法返回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.

我的Javascript:

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

我的控制器:

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

推荐答案

@Dimi的评论向我展示了发生了什么:在函数名称的开头使用下划线,例如 _function_one 不起作用; function_one 确实起作用,使CodeIgniter中断.

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.

我想出的解决方案是将函数重命名为 function_one 格式(就我而言,是 get_procs )并在 routes上创建规则.php 配置文件:

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

这是一个解决方法,可以在不更改CodeIgniter的默认配置的情况下正常工作.我不知道还有没有其他办法.

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

@ cssBlaster21895指出,CodeIgniter遵循PHP编码样式(请参阅更多此处),它将 _function 确定为私有函数.

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

这篇关于Codeigniter-Ajax调用时出错(404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆