Newbee_Mofi
2020-12-03 20:39
采纳率: 66.7%
浏览 400
已采纳

如何封装这个json,让其符合LayUI前端的JSON格式

应该如何更改前端代码,目前提示“返回的数据不符合规范”

http://127.0.0.1/conn.php  :

[{"comcname_d":"\u9ec4\u5357","yjpk_d":"2417047.29","sl":"721"},{"comcname_d":"\u6d77\u4e1c","yjpk_d":"13843905.83","sl":"1203"},{"comcname_d":"\u6d77\u5317","yjpk_d":"6153061.52","sl":"1857"},{"comcname_d":"\u6d77\u5357","yjpk_d":"1132748.40","sl":"412"},{"comcname_d":"\u7389\u6811","yjpk_d":"9625683.90","sl":"568"},{"comcname_d":"\u6d77\u897f","yjpk_d":"5032555.34","sl":"1978"},{"comcname_d":"\u683c\u5c14\u6728","yjpk_d":"2475259.80","sl":"506"},{"comcname_d":"\u897f\u5b81","yjpk_d":"28547051.91","sl":"5729"},{"comcname_d":"\u679c\u6d1b","yjpk_d":"374000.80","sl":"241"}]

前端:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>table模块快速使用</title>
		<link rel="stylesheet" href="/layui/css/layui.css" media="all">
	</head>
	<body>

		<table id="demo" lay-filter="test"></table>

		<script src="/layui/layui.js"></script>
		<script>
			layui.use('table', function() {
				var table = layui.table;

				//第一个实例
				table.render({
					elem: '#demo',
					height: 312,
					url: "http://127.0.0.1/conn.php" //数据接口
						,
					
					cols: [
						[ //表头
							{field: 'comcname_d',title: '地市名',width: 80,sort: true,fixed: 'left'},
							{field: 'yjpk_d',title: '已决金额',width: 100,sort: true,//排序符号 
							}, 
							{field: 'sl',title: '已决件数',width: 100,sort: true}
						]
					]
				});

			});
		</script>
	</body>
</html>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • 黑牛儿 2020-12-06 16:34
    已采纳

    index.html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>table模块快速使用</title>
        <link rel="stylesheet" href="../static/layui/css/layui.css" media="all">
    </head>
    <body>
    
    <table id="demo" lay-filter="test"></table>
    
    <script src="../static/layui/layui.js"></script>
    <script>
        layui.use('table', function() {
            var table = layui.table;
    
            //第一个实例
            table.render({
                elem: '#demo',
                height: 312,
                url: "./index.php",
    
                cols: [
                    [ //表头
                        {field: 'comcname_d',title: '地市名',width: 80,sort: true,fixed: 'left'},
                        {field: 'yjpk_d',title: '已决金额',width: 100,sort: true,//排序符号
                        },
                        {field: 'sl',title: '已决件数',width: 100,sort: true}
                    ]
                ]
            });
    
        });
    </script>
    </body>
    </html>

    index.php

    <?php
    $data = "[{\"comcname_d\":\"\u9ec4\u5357\",\"yjpk_d\":\"2417047.29\",\"sl\":\"721\"},{\"comcname_d\":\"\u6d77\u4e1c\",\"yjpk_d\":\"13843905.83\",\"sl\":\"1203\"},{\"comcname_d\":\"\u6d77\u5317\",\"yjpk_d\":\"6153061.52\",\"sl\":\"1857\"},{\"comcname_d\":\"\u6d77\u5357\",\"yjpk_d\":\"1132748.40\",\"sl\":\"412\"},{\"comcname_d\":\"\u7389\u6811\",\"yjpk_d\":\"9625683.90\",\"sl\":\"568\"},{\"comcname_d\":\"\u6d77\u897f\",\"yjpk_d\":\"5032555.34\",\"sl\":\"1978\"},{\"comcname_d\":\"\u683c\u5c14\u6728\",\"yjpk_d\":\"2475259.80\",\"sl\":\"506\"},{\"comcname_d\":\"\u897f\u5b81\",\"yjpk_d\":\"28547051.91\",\"sl\":\"5729\"},{\"comcname_d\":\"\u679c\u6d1b\",\"yjpk_d\":\"374000.80\",\"sl\":\"241\"}]";
    $data = json_decode($data, true);
    $result['data'] = $data;
    $result['code'] = 0;
    $result['msg'] = "请求成功";
    
    echo json_encode($result, JSON_UNESCAPED_UNICODE);

    望采纳!!!!

    评论
    解决 无用
    打赏 举报
查看更多回答(4条)

相关推荐 更多相似问题