Codeigniter and AJAX retrieving/getting data from database error

大憨熊 提交于 2019-12-25 17:14:08

问题


I always land into the error function in ajax, what is the problem here?

I want to retrieve a img url stored in the database and append it using jquery to show my img gallery. But before I can do any of this, I have to make sure it always land on the success function, but I always land on error function... so I can not proceed and I am stuck here,

UPDATE:

check the console, it says SyntaxError: missing : after property id on success:function(result)

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)
            {
                alert("success");
            },
            error:function()
            {
                alert("failed"); //it always show the alert failed.

            }
        });
    }

My controller:

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

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

    $json = $this->design->load_gallery($user_id->id);
    echo json_encode($json);
}

My model:

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

回答1:


You will land in success function when server answer to you with 2xx http status.
If you always land on error function you have an error in codeigneter.
Try to get http status from you server (browser console will help you) and it will help you to understand where is problem.




回答2:


You should create first global variable in footer

var base_url = "<?= base_url(); ?>";

And now you need to use like this

function load_contents(track_page)
    {
        $('#loading').show();
        var ref = new Date(); 
        var url = base_url + "gallery/load_design?=" + ref.getTime();
        $.ajax({
            url:url,
            type:'GET',
            cache: false,
            success:function(result)
            {
                alert("success");
            },
            error:function()
            {
                alert("failed"); //it always show the alert failed.

            }
        });
    }


来源:https://stackoverflow.com/questions/41683522/codeigniter-and-ajax-retrieving-getting-data-from-database-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!