dongshang6062 2013-01-05 12:40
浏览 52
已采纳

使用CodeIgniter调用控制器失败的AJAX

I've started my first CodeIgniter project and am having a lot of trouble making an Ajax call to my controller. I've put a simple echo statement in the controller but am getting a console error in the browser - POST http://localhost:8888/lotto/get_results/ 404 (Not Found). This leads me to believe that i'm not referencing the controller properly in the AJAX call. Below is the relevant code.

View - index.php

$(document).ready(function(){
    $('#notification').hide();
    retrieveValues();
});

$('.numDraws').change(function(){
    retrieveValues();
});

function retrieveValues() {
    if (!checkConnection()) {
        $('#notification').html("<span>No internet connection available</span>");
        $('#notification').slideDown(500, 'linear');
        return;
    } else {
        $('#notification').slideUp(500, 'linear');
        $('#loading').fadeIn(200);
        var numOfDraws = parseInt($('.numDraws').find('option:selected').val());
        if (isNaN(numOfDraws)) {
            numOfDraws = "ALL"; 
        }
        $.ajax({
            url: "/lotto/get_results/",
            type: "post",
            data: {numOfDraws:numOfDraws},
            success: function (data) {
                // var json = $.parseJSON(data);
                // setTimeout(function(){displayResults(json)} ,1200);
                alert(data);
            }
        }); 
    }

} 

Controller - lotto.php

<?php 

class Lotto extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('lotto_model');
    }

    public function index()
    {
        $data['title'] = "Home";

        $this->load->view('templates/header', $data);
        $this->load->view('lotto/index');
        $this->load->view('templates/footer');

    }

    public function get_results($numOfDraws) {
        //$data['results'] = $this->lotto_model->get_results(1);
        echo "Reached the controller";
    }
}
?>

Also in my config file i've got the following:

$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

Any help would be appreciated - i've spent a lot of time on this but can't seem to figure it out.

  • 写回答

1条回答 默认 最新

  • douna4762 2013-01-05 12:51
    关注

    You're facing an error probably because the method expects an argument you're not providing (and the router can't work the call right). Try this 2 things:

    1) crate an url using the built-in functions (to avoid problems with that):

    url: "<?php echo site_url('lotto/get_results');?>"
    

    2) Since the method looks like should receive a POST variable, and not a GET one, you need to fetch it the right way:

    public function get_results() {
            $numOfDraws = $this->input->post('numOfDraws');
            //do something with $numOfDraws here
            echo $numOfDraws; // just to check the value is being passed
        }
    

    Passing an argument to the method works if the variable comes from an HTTP GET request, which is not your case. If that's your intention, instead, you need to remove the "POST" type in the AJAX call and provide a value while building the AJAX url. Somethng like

    url: "<?php echo site_url('lotto/get_results');?>/"+numOfDraws;
    

    In this case, your method would be get_result($draws) , with the parameter

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?