简体   繁体   中英

Codeigniter and AJAX console.log(result) shows undefined

Console.log(result) shows undefined when I open my console in firefox. Is there something wrong with the returned json?

Here is my script:

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

My controller:

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

My model:

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

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

Try this..

Controller

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

Model:

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

Try returning from your model like this:

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

concatnation is wrong change url single quotes to double quotes.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM