Minoru
Minoru

Reputation: 1730

Codeigniter - Error on Ajax call (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");
}

Upvotes: 0

Views: 860

Answers (1)

Minoru
Minoru

Reputation: 1730

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.

Upvotes: 1

Related Questions