dongwolu5275 2014-08-04 04:58
浏览 75
已采纳

使用jQuery AJAX请求CodeIgniter显示从Controller到View的数据库

I am having a problem in fetching data from database using jQuery AJAX as json datatype I have read and tried JSON.parse in jQuery but it did not work. All I am returning in console.log(data) is object and the data from database in object but I don't know how to show it on view. Any suggestions please.

Here is my Controller.

  function fetch(){
                $data_fetch     = array(
                    'id'        => $this->input->post('txt_id')
                    );
                $data['records'] = $this->form_model->fetch_model($data_fetch);
        $results    = json_encode($data);

            $this->output->set_content_type('application/json');
            $this->output->set_output($results);
        }

Here is my jQuery AJAX

$.ajax({
            type    : 'POST',
            url     :  base_url + 'index.php/form_controller/fetch',
            data    : {txt_id : id },
            dataType : 'JSON',
            success : function(data){
                // var re = JSON.parse(data);
                // var re = $.parseJSON(data);
                // var data = JSON.parse(data);
                alert(data.name);
                console.log(data);

            },
            error   : function(data){
                console.log('erronr in fetch');
            }
        });

The data.name is showing undefined in alert. Any help will be appreciated.

  • 写回答

1条回答 默认 最新

  • douan0729 2014-08-04 05:08
    关注

    You don't need to use JSON.parse the variable data is already a JSON, so just use it as a you would with any other JSON.

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

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面