今天使用uniapp的uni.request方法 传递一个数组 后台居然一直读取不到 要么就直接返回[object object]

查文档发现 uniapp会强制把数组转为字符串 不知道是不是这个原因 折腾半天 终于找到解决办法:
前端:


我是传入从另外一个接口获取的ids 此处还因为uni.request() 异步的原因 只能获取到 {__ob__: Observer}
此处需要把ids序列化一下 变成json字符串 再做传递 像这样:
JSON.stringify(this.ids)
后端PHP做法:
php如果不做处理 直接输出会变成这样

所以我们需要给他做 转义引号、 清除反斜杠的处理

后端需要使用html_entity_decode 转义 使用 stripcslashes 清除反斜杠 最后再使用json_decode转成具体对象就行了
json_decode(stripcslashes(html_entity_decode($info['ids'])),true);
这里结尾的true是 返回值是数组,否则返回值为object
最后就直接可以使用了

网友评论