小程序笔记
wx.request({ url: 'http://...www/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) } })
需要注意的是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
通过content-type 得到传参方式 最下方仅为post的数据,我没农商 一般来说传参方式分为 application/json multipart/form-data application/x-www-form-urlencoded 为什么会有这么多传参方式,就是因为他的编码方式,协议规定 POST 提交的数据必须放在消息主体(entity-body)中,但协议并没有规定数据必须使用什么编码方式。 数据发送出去,还要服务端解析成功才有意义。一般服务端语言如 php、python 等,以及它们的 framework,都内置了自动解析常见数据格式的功能。 服务端通常是根据请求头(headers)中的 Content-Type 字段来获知请求中的消息主体是用何种方式编码,再对主体进行解析。 所以说到 POST 提交数据方案,包含了 Content-Type 和消息主体编码方式两部分。
————————————————
版权声明:本文为CSDN博主「lengxin337」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lengxin337/article/details/78234503