小编给大家分享一下小程序开发中如何实现POST请求,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
之前的post的请求确实有不少的坑,公测以来小程序一直在更新,所以之前的版本只能作废掉,重新写一次
对于小程序的get请求就不多说了,因为文档都有例子ctrl+c ctrl+v就好了
1.加上 method: "POST"
,不区分大小写
2-1.普通参数传递需加上'content-type': "application/x-www-form-urlencoded"
(这里的content-type跟Content-Type现在效果一样,之前得写成全小写)
2-2.json对象参数传递需加上'content-type': "application/json"
index.js
var app = getApp() Page({
data: {
id: '',
username: '',
age: ''
},
user2json: function() {
var that = this;
wx.request({
url: 'http://localhost:8080/springMVC/user/bean2json.mn',
data: {
id: 1,
username: "toBeMN",
age: 38
},
method: 'POST',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function(res) {
that.setData({
id: res.data.id,
username: res.data.username,
age: res.data.age
})
}
})
},
onLoad: function() {}
})
index.wxml
<?xml version="1.0" encoding="utf-8"?>
<view class="container">
<button bindtap="user2json">bean2json</button>
<text>id:{{id}}</text>
<text>username:{{username}}</text>
<text>age:{{age}}</text>
</view>
看完了这篇文章,相信你对“小程序开发中如何实现POST请求”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。