doukougua7873 2019-02-05 12:28
浏览 127

codeigniter没有返回json / ajax响应

I have read a lot on codeIgniter ajax response. I have and modified my ajax script multiple times yet codeIgniter does not return the json response although I see the response when debugging with firefox developer browser under the network tab of web console. here's what i have written

AJAX script

$("#class_id").change(function(){

    var class_id = $("#class_id").val();
    alert(class_id);
    alert("<?php echo base_url(); ?>teacher/index.php?teacher/set_class_id");
        $.ajax({
            method: "POST",
            url: "<?php echo base_url(); ?>teacher/index.php?teacher/set_class_id",
            dataType: 'json',
            data: {class_id: class_id},

            success: function(response) {
                $("#res").html(reponse.class_id);
            }
    });
    return false;

});

controller

function set_class_id()
{
    if ($this->session->userdata('teacher_login') != 1)
        redirect(base_url(), 'refresh');

        //echo "dsdsd".$this->input->POST('class_id');
        if (!empty($this->input->POST('class_id')))
        {
            $page_data = array('class_id' => $this->input->POST('class_id'));
            //$response["JSON"] = json_encode($page_data);
            $response = array('class_id' => $this->input->POST('class_id'));

            echo json_encode($page_data);

            $this->load->view('backend/index', $page_data);

        }
}
  • 写回答

2条回答 默认 最新

  • douyonglang4845 2019-02-05 12:36
    关注

    Have you tried using

    $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode(array('foo' => 'bar')));
    

    Also, check th output class for more info Codeigniter Output Class

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站