需要注意的是 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
- ---------------------
作者: lengxin337
来源: http://www.jianshu.com/p/cd98f6b1576d