当前位置:首页 > Web开发 > 正文

JQuery 处理 微擎传递过去数据

2024-03-31 Web开发

JQuery 处理 微擎传递过去数据

PS:微擎得到的数据大多数是数组(我们这里处理数组)

将数组使用 json_encode() 函数处理成 JSON 格式

前端在 script 中使用 引号 将变量括起来即可得到后台设定的数据

使用 $.parseJSON() 函数转换成jquery可以处理的对象

实例:

后台:

$shops = json_encode(array([‘id‘ => 12, ‘name‘ => ‘GetcharZp‘], [‘id‘ => 13, ‘name‘ => ‘GetcharMcx‘]));

前台:

var shops = $.parseJSON(‘{$shops}‘); // 注意,这里要用单引号,因为转JSON的字符都是双引号 console.log(shops); for (var i = 0; i < shops.length; ++ i) { console.log(shops[i][‘id‘]); }

JQuery 处理 微擎传递过去数据

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/42613.html