为什么总是提示数据接口请求异常?各位大神!!

提问 未结
18 1636
叒禧
叒禧 2017-12-31
悬赏:20飞吻
版本:layui 浏览器:
后台代码
public function messageAjax(){
$message = M('message');
import('ORG.Util.Page');
//$User = M('article');
$count = $message->order("id desc")->count();
$Page = new \Think\Page($count, 10);//
$show = $Page->show();
$list=$message->field('id')->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();

$count = $message->order("id desc")->count();

$list=array('code'=>0,'msg'=>"",'count'=>(int)$count,'data'=>$list);
$list=(object)$list;
$this->ajaxReturn($list);
}

前台代码
layui.use('table', function(){
var table = layui.table;
table.render({
elem: '#test'
,height: 315
,url: 'messageAjax' //数据接口
,page: false//开启分页
,cols: [[ //表头
{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
// ,{field: 'username', title: '用户名', width:80}
// ,{field: 'email', title: '性别', width:80, sort: true}
// ,{field: 'tel', title: '城市', width:80}
// ,{field: 'pubdate', title: '签名', width: 170}
]]
});

});
//后台返回数据
{code: 0, msg: "", count: 7,…}
code:
0
count
:
7
data
:
[{id: "123"}, {id: "122"}, {id: "121"}, {id: "120"}, {id: "119"}, {id: "118"}, {id: "113"}]
msg
:
""
回帖
  • 四两
    2017-12-31
    JSON格式不对, 所有得KEY 必须加引号亲 {id: "123"} 改为 {"id": "123"}
    1 回复
  • 叒禧
    2017-12-31
    @四两 好好 我试试
    0 回复
  • 叒禧
    2017-12-31
    {code: 0, msg: "", count: 7,…}
    code
    :
    0
    count
    :
    7
    data
    :
    "[{"id":"123"},{"id":"122"},{"id":"121"},{"id":"120"},{"id":"119"},{"id":"118"},{"id":"113"}]"
    msg
    :
    ""
    0 回复
  • 叒禧
    2017-12-31
    加上双引号不管用
    0 回复
  • zilong
    2017-12-31
    不能用return, 直接echo json_encode($list,JSON_UNESCAPED_UNICODE);
    0 回复
  • zilong
    2017-12-31
    $list=(object)$list;
    $this->ajaxReturn($list);
    这两行不要 直接echo json_encode
    0 回复
  • 叒禧
    2017-12-31
    返回信息:
    {"code":0,"msg":"","count":7,"data":[{"username":"shuangxi"},{"username":"shuangxi"},{"username":"shuangxi"},{"username":"13370140101"},{"username":"13370140101"},{"username":"13370140101"},{"username":"13370140101"}]}
    后台代码

    $message = M('message');

    import('ORG.Util.Page');
    $count = $message->order("id desc")->count();
    $Page = new \Think\Page($count, 10);//
    $show = $Page->show();
    $list=$message->field('username')->order("id desc")->select();
    $count = $message->order("id desc")->count();

    $list=array('code'=>0,'msg'=>"",'count'=>(int)$count,'data'=>$list);
    echo json_encode($list);exit();

    还是数据接口请求异常
    0 回复
  • 叒禧
    2017-12-31
    @zilong 返回信息:
    {"code":0,"msg":"","count":7,"data":[{"username":"shuangxi"},{"username":"shuangxi"},{"username":"shuangxi"},{"username":"13370140101"},{"username":"13370140101"},{"username":"13370140101"},{"username":"13370140101"}]}
    后台代码

    $message = M('message');

    import('ORG.Util.Page');
    $count = $message->order("id desc")->count();
    $Page = new \Think\Page($count, 10);//
    $show = $Page->show();
    $list=$message->field('username')->order("id desc")->select();
    $count = $message->order("id desc")->count();

    $list=array('code'=>0,'msg'=>"",'count'=>(int)$count,'data'=>$list);
    echo json_encode($list);exit();

    还是数据接口请求异常
    0 回复
  • zilong
    2017-12-31
    public function showSupplierTb(){
    $limit=input('limit');
    $start = setPage(input('page'),$limit);
    $tb = Db::name('company')->limit("$start,$limit")
    ->order('id desc')->select();
    $row = Db::name('company')->count();
    $data=['code'=>0,'msg'=>'','count'=>$row,'data'=>$tb];
    echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }
    0 回复
  • zilong
    2017-12-31
    tp3.2的M模型查询数据库$list是对象还是二维数组?返回给表格的数据的data对应值必须是二维数组,再使用json_encode 在Tp5中Db查询的结果返回值是,二维数组
    0 回复
  • zilong
    2017-12-31
    0 回复
  • 叒禧
    2017-12-31
    @zilong

    M模型查询数据库$list是二维数组
    0 回复
  • 叒禧
    2017-12-31


    返回数据

    @zilong
    0 回复
  • zilong
    2017-12-31
    {"code":0,"msg":"","count":11,"data":[{"id":12,"supplier":"C.BAMA","email":"","phone":"","address":"","contact":""},{"id":10,"supplier":"C.FATIGATI","email":"","phone":"","address":"","contact":""},{"id":9,"supplier":"C.ARIX","email":"","phone":"","address":"","contact":""},{"id":8,"supplier":"C.DARICO","email":"","phone":"","address":"","contact":""},{"id":7,"supplier":"C.VECA","email":"veca5566@gmail.com","phone":"","address":"","contact":"luna"},{"id":6,"supplier":"C.MJ","email":null,"phone":null,"address":null,"contact":"王杰"},{"id":5,"supplier":"C.CP","email":null,"phone":null,"address":null,"contact":"MARCHLE"},{"id":4,"supplier":"C.LAMPA","email":"dsfsdf@gmail.com","phone":null,"address":null,"contact":"JIM"},{"id":3,"supplier":"C.LUBEX","email":null,"phone":null,"address":null,"contact":"LUCY"},{"id":2,"supplier":"C.ITC","email":"itc888@gmail.com","phone":null,"address":null,"contact":"JACK"}]}
    0 回复
  • zilong
    2017-12-31
    前台代码
    layui.use('table',function(){
    var table = layui.table;
    var comIns= table.render({
    elem:'#supplier'
    ,height: 473
    ,cols: [[ //表头
    {field: 'id',align:'center', title: 'ID',width:60}
    ,{field: 'supplier',style:"font-weight:bolder;color:red", align:'center',title: '供货商', width:110}
    ,{field: 'email',width:150,style:"color: #5FB878;",edit:true,align:'center', title: '邮箱'}
    ,{field: 'phone',width:150,style:"color: #5FB878;",edit:true,align:'center', title: '电话'}
    ,{field: 'address',width:155,style:"color: #5FB878;",edit:true,align:'center', title: '地址'}
    ,{field: 'contact',align:'center',style:"color: #5FB878;",edit:true,width:100, title: '联系人'}
    ,{width:190, align:'center',title: '操作', toolbar: '#supplierBar'}
    ]]
    ,url:"showSupplierTb"
    ,page:true
    })
    0 回复
  • 叒禧
    2017-12-31
    <table class="layui-hide" id="test" lay-filter="demo"></table>
    <script src="/Public/layui/layui.js"></script>
    <script>
    layui.use('table', function(){
    var table = layui.table;
    //第一个实例
    table.render({
    elem: '#test'
    ,height: 315
    ,url: 'messageAjax' //数据接口
    ,method:'get'
    ,page: true//开启分页
    ,cols: [[ //表头
    {field: 'username', title: '用户名', width:80}
    ]]
    });
    });
    </script>

    我是这样写的 ,方便加qq,我的qq 782056221
    0 回复
  • 叒禧
    2017-12-31
    @zilong

    返回数据多出一个小红点
    0 回复
  • 叒禧
    2017-12-31
    已经解决了,就是thinkphp3.2出现红点问题,

    数据能展现出来,谢谢大家帮忙@zilong,@四两
    0 回复
本帖已设置禁止回复