1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 微信小程序wx.request使用post方式传参

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

时间:2023-07-11 10:20:27

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。