1. 程式人生 > >微信小程式wx.request使用post方式傳參

微信小程式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