【问题标题】:Json Data print after insert a record using Jquery Ajax in CodeIgniter在 CodeIgniter 中使用 Jquery Ajax 插入记录后打印 Json 数据
【发布时间】:2019-12-07 16:07:43
【问题描述】:

我在 Codeigniter 中使用 Jquery Ajax,通过引导模式注册用户后,我得到如下 JSON 数据,而不是从表中获取数据。我哪里错了?请帮忙!

{"成功":true,"type":"添加"}

这是我的 Jquery 函数

```
<script>
$(function(){
    display();
    //add new

function display(){
    $.ajax({
        type:'ajax',
        url:'<?php echo base_url()?>frame_cont/display',

        async: false,
        dataType:'json',
        success:function(data){
            var html='';
            var i;
            for(i=0;i<data.length;i++){

                html +='<tr>'+
                            '<td>'+data[i].id+'</td>'+
                            '<td>'+data[i].username+'</td>'+
                            '<td>'+data[i].email+'</td>'+
                            '<td>'+data[i].password+'</td>'+
                            '<tr>';
            }
            $('#showdata').html(html);
        },
        error:function(){
            alert('Could not get Data from Database');
        }
});
};
});
</script>```

这里是控制器类

<?php 
class Frame_cont extends CI_Controller{
    public function index(){
        $this->load->view('index');
    }

    public function register(){
        $this->load->model('frame');
        $result=$this->frame->insert();
        $msg['success']=false;
        $msg['type']='add';
        if($result){
            $msg['success']=true;
        }
        echo json_encode($msg);
        }




    public function display(){
        $this->load->model('frame');
        $result=$this->frame->display();
        echo json_encode($result);
    }
}

这就是代码的全部内容,对不起英文

【问题讨论】:

    标签: ajax codeigniter


    【解决方案1】:

    在您的注册功能中,您最后执行echo json_encode($msg);。你应该在 if 条件中回显插入的值

    【讨论】:

      【解决方案2】:

      出现问题是因为我在表单按钮中使用 type="submit" 而不是 type="button"。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 2018-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多