l***@163.com
l***@163.com
  • 发布:2021-02-07 11:11
  • 更新:2021-02-07 11:11
  • 阅读:579

uni.request 传送复杂数据

分类:uni-app

一、需求:下拉列表筛选显示对应的数据

二、应用到的组件:
dropDown下拉菜单:https://ext.dcloud.net.cn/plugin?id=2650
mescroll:http://www.mescroll.com/uni.html

三、前端代码
uni.request只能传递字符串,但是e.contentActiveIndexList是json对象。将json数据转换为字符串,才能采用uni.request将数据传送到后台。

async onItemSelect(e){  
    this.course_select = JSON.stringify(e.contentActiveIndexList);        //将json数据转换为字符串,才能采用uni.request    
        this.upCallback(this.page);  
},  

let res = await this.$myHttp.post({  
        url: this.$myHttp.urlMap.course_select,  
        data: {  
            course_select: this.course_select,  
        },  
        needLogin: true  
})

转换后发数据形式是:{"headerIndex":0,"index":0},{"headerIndex":1,"index":2},{"headerIndex":2,"index":0}

四、PHP后端代码
再到后台接收数据时变成了这种形式:{"headerIndex":0,"index":0}
需要用到php的htmlspecialchars_decode()函数进行转义
以下代码可以获取到第一组数据的index的值:
json_decode(htmlspecialchars_decode($course_select), true)["0"]["index"]

五、参考文档
https://www.w3schools.com/js/js_json_stringify.asp
http://www.edbiji.com/doccenter/showdoc/4/nav/307.html
https://blog.csdn.net/milli236/article/details/79042713

有更好的思路吗?:)

0 关注 分享

要回复文章请先登录注册