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

来源:互联网 发布:淘宝客服子取名字大全 编辑:程序博客网 时间:2024/05/13 07:06

需要注意的是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 pin json) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
}
return str.join("&");
}
module.exports.json2Form = json2Form


原创粉丝点击