微信小程序POST请求后台获取不到请求的参数

这是一个测试的请求接口,大家可以用来测试
请求测试地址:http://www.qiaoming.online/requestdemo/login
请求方式:post
请求参数:  name:"admin",        pwd:"123456"
请求头: 'content-type': 'application/json'
请求成功返回值:{"flag":"200","message":"请求成功!","data":null}
请求失败返回值:{"flag":"401","message":"请求失败!","data":null}
--------------------- 

这是我一开始写的请求的demo

wx.request({
      url: 'http://192.168.1.123/StudentManage/jsdemo.php', 
      method:'post',
      data: {
        username: 'yaodan',
        password: '123456'
      },
      header: {
	      'content-type': 'application/json' // 默认值
	  },
      success: function (res) {
        console.log(res.data)
      }
    })

但是后台怎么都获取不到yaodan和123456,后来发现是header写的有问题,微信小程序默认值是'content-type': 'application/json' ,你需要改成'content-type': 'application/x-www-form-urlencoded'
这是最终请求的结果

wx.request({
      url: 'http://192.168.1.123/StudentManage/jsdemo.php', 
      method:'post',
      data: {
        username: 'yaodan',
        password: '123456'
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(res.data)
      }
    })
posted @ 2018-05-15 14:50  我的网名  阅读(41)  评论(0)    收藏  举报