1. 程式人生 > 其它 >微信小程式:Failed to read the ‘responseText‘ property from ‘XMLHttpRequest‘

微信小程式:Failed to read the ‘responseText‘ property from ‘XMLHttpRequest‘

技術標籤:小程式javascript

開發微信小程式的過程中,用wx.request獲取伺服器的資料時,發現總是收不到伺服器傳來的json資料
錯誤程式碼如下:

  wx.request({
    url: 'http://localhost:8000/v1/quote',
    data: {},
    header: {
      'content-type': 'application/json'
    },      
    responseType : 'json',
    success (res) {
      console.log(res)
      that.setData
({ motto: res.data.content+"\n------"+res.data.author })

報錯資訊:
Uncaught DOMException: Failed to read the ‘responseText’ property from ‘XMLHttpRequest’: The value is only accessible if the object’s ‘responseType’ is ‘’ or ‘text’ (was ‘json’).
截圖:
報錯資訊圖片
解決方案:
仔細讀報錯可以發現,wx.request中的“responseType”項只有兩種選項,’‘或者’text’(之前一直用的“json”)

將json修改成text就可以正常收到資料了
ps:應該是微信最近改的規則,前兩天都還可以用json來著,今天突然就報錯了……