1、微信小程序递交
var orders = new Array(); // 生成模拟数据 if (orders.length == 0) { var obj0 = new Object(); obj0.id = 1; obj0.name = 'CTS项目'; obj0.pic = '../../images/1.png'; obj0.quantity = 3; obj0.nowPrice = 77; obj0.selected = true; orders[0] = obj0; var obj1 = new Object(); obj1.id = 2; obj1.name = 'WMS项目'; obj1.pic = '../../images/2.png'; obj1.quantity = 5; obj1.nowPrice = 66; obj1.selected = true; orders[1] = obj1; } // 调用测试接口 wx.request({ url: urlList.testUrl, data: { 'orders': orders, }, header: { 'content-type': 'application/json', 'Cookie': 'PHPSESSID=' + app.globalData.session_id }, success: res => { console.log(res); }, fail: res => { console.log('errMsg' + ':' + res) } });
2、PHP服务端接收的原始数据
[{\"id\":1,\"name\":\"CTS项目\",\"pic\":\"../../images/1.png\",\"quantity\":1,\"nowPrice\":77,\"selected\":true},{\"id\":2,\"name\":\"WMS项目\",\"pic\":\"../../images/2.png\",\"quantity\":2,\"nowPrice\":66,\"selected\":true}]
3、过滤转义符。转换成数组。
$orders = $_GET['orders']; // 去除字符串中的反斜线字符 $temp = stripslashes($orders); // 返回值默认是JSON对象,当第二个可选参数是TRUE的时候,则返回的是数组 $arr = json_decode($temp, true); for($i = 0; $i < count($arr); $i++) { echo('arr3' . ' ' . $arr[$i]['id'] . ' ' . $arr[$i]['name']); }