微信小程序wx.request使用post方式传参

需要注意的是method是get方式的时候,header为{"Content-Type": 'application/json'},当method为post时,header为{"Content-Type": "application/x-www-form-urlencoded"}

post方式传递的参数需要转换

1。js中

var util= require( '../../utils/util.js');

page({

loginSubmit: function(e){
console.log(e.detail.value.username),
console.log(util.formatTime),
wx.request({
url: 'http://127.0.01:8000/runxiang_yiyao/Mobile/Index/login',
method: 'post',
data: util.json2Form({
username: e.detail.value.username,
password: e.detail.value.password,
}),

header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(util.formatTime)
// console.log(res)
}
})
}

})


2. 在util中定义函数json2Form

function json2Form(json) {
var str = [];
for ( var p in json) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
}
return str.join( "&");
}
module.exports.json2Form = json2Form


你可能感兴趣的:(微信小程序wx.request使用post方式传参)