小程序在使用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) } })
這樣就能成功請求到數據了。