之前的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请求的详细内容。更多信息请关注PHP中文网其他相关文章!