微信小程序wx.request实现后台数据交互功能分析

网友投稿 476 2023-11-14

本文实例讲述了微信小程序wx.request实现后台数据交互功能。分享给大家供大家参考,具体如下:

微信小程序wx.request实现后台数据交互功能分析

记录微信小程序wx.request这个api在跟后台交互时遇上的问题。

1、根据资料,完成第一步,请求发送,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
wx.request({
url: https://localhost:8443/xiaochengxu/addBill.do,
data: e.detail.value,
method: POST,
success:function(res) {
console.log(submit success);
},
fail:function(res){
console.log(submit fail);
},
complete:function(res){
console.log(submit complete);
}
})

后台成功接收到请求,控制台也打印了submit success和submit complete,但是,后台请求并未接收到数据,打开调试,发现数据都在request payload中,所以后台无论是springmvc的映射bean还是req.getParameter都拿不到参数。

解决方法参考本站:https://www.jb51-/article/129039.htm

简单说就是增加了header: {content-type: application/x-www-form-urlencoded},后台成功获取数据。

至此,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wx.request({
url: https://localhost:8443/xiaochengxu/addBill.do,
data: e.detail.value,
method: POST,
header: {content-type: application/x-www-form-urlencoded},
success:function(res) {
console.log(submit success);
},
fail:function(res){
console.log(submit fail);
},
complete:function(res){
console.log(submit complete);
}
})

2、接收请求返回数据

这一步问题不大,我是按照json格式返回的,只是按照官网写的console.log(res.data)的话,会在控制台打印Object,带上参数名就好了,比如res.data.code

希望本文所述对大家微信小程序开发有所帮助。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:怎么制作属于自己的app
下一篇:自己如何开发app软件?开发周期,费用需要多少
相关文章

 发表评论

暂时没有评论,来抢沙发吧~