Codeigniter和AJAX console.log(结果)显示未定义

时间:2017-01-16 21:31:00

标签: javascript php jquery ajax codeigniter

当我在firefox中打开控制台时,Console.log(结果)显示未定义。返回的json有什么问题吗?

这是我的剧本:

function load_contents(track_page) {
    $('#loading').show();
    $.ajax({
        url:'<?php echo base_url('gallery/load_design');?>',
        type:'GET',
        dataType:'json',
        success:function(result) {
            console.log(result);
            alert("success");
        },
        error:function(result) {
            console.log(result);
            alert("failed");
        }
    });
}

我的控制器:

public function load_design() {
    $this->load->model('design');
    $this->load-model('profile');

    $user_id = $this->profile->retrieve_userid();

    $result = $this->design->load_gallery($user_id->id);

    header('Content-Type: application/json');
    echo json_encode($result);
}

我的模特:

function load_gallery($user_id) {
    $data = array();

    $query = $this->db->query("SELECT * from designs WHERE user_id = '".$user_id."' LIMIT 9");
    return $query->result();
}

3 个答案:

答案 0 :(得分:0)

试试这个..

控制器

public function load_design()
{
    $this->load->model(array('design','profile'));

    $user_id = $this->profile->retrieve_userid();

    $result = $this->design->load_gallery($user_id->id);

    header('Content-Type: application/json');
    echo json_encode($result);
}

型号:

function load_gallery($user_id)
    {
        $data = array();

        $this->db->where('user_id',$user_id);
        $query = $this->db->get('designs',9,0);
        return $query->result_array();
    }

答案 1 :(得分:0)

尝试从您的模型中返回:

$query = $this->db->query("SELECT * from designs WHERE user_id = '".$user_id."' LIMIT 9");
return $query->result_array();

答案 2 :(得分:0)

concatnation是错误的更改url单引号加双引号。

function load_contents(track_page) {
$('#loading').show();
$.ajax({
    url:"<?php echo base_url('gallery/load_design');?>",
    type:'GET',
    dataType:'json',
    success:function(result) {
        console.log(result);
        alert("success");
    },
    error:function(result) {
        console.log(result);
        alert("failed");
    }
});

}