小程序的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