JQuery 处理 微擎传递过去数据

JQuery 处理 微擎传递过去数据

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

  1. 将数组使用 json_encode() 函数处理成 JSON 格式
  2. 前端在 script 中使用 引号 将变量括起来即可得到后台设定的数据
  3. 使用 $.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']);
}