小程序的get和post请求头的区别


小程序在使用wx.request()接口 时 header 请求头默认是这样的

 

wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'content-type': 'application/json' // 默认值
  },
  success: function(res) {
    console.log(res.data)
  }
})

 

  当然如果没设置请求类型是能请求到数据的,但是如果设置为POST请求,再使用 'content-type': 'application/json' // 默认值 就坑爹了,这样是请求不到想要的数据的。此时要更改请求头

 

wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
     x: '' ,
     y: ''
  },
  method:"POST",
  header: {
       "Content-Type": "application/ x - www - form - urlencoded" //post请求设置
  },
  success: function(res) {
    console.log(res.data)
  }
})

 

  这样就能成功请求到数据了。

 

 

 




					


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM