鍍金池/ 問(wèn)答/HTML/ axios catch錯(cuò)誤

axios catch錯(cuò)誤

    this.axios({
      method: 'POST',
      url: URL,
      data: {
        "mobile": tel,
        "auth_code": code,
        "lngLat": this.lngLat,
        "regSource": 'H5',
        "frc": "H5",
        "ref": this.ref
      },
      headers: {
        'appId': config.config.APPID,
        'token': this.token
      }
    })
      .then(function (response) {
        console.log(response)

      })
      .catch(function (error) {
        console.log(error)
      });
      
      
      console.log(error)里邊輸出
      
      
    Error: Request failed with status code 400
    at createError (createError.js?16d0:16)
    at settle (settle.js?db52:18)
    at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)
    at _this.(anonymous function) (chrome-        extension://jegnjmcegcpodciadcoeneecmkiccfgi/enablecrossorigin.js:81:55)
    
    
    這樣寫(xiě)哪里錯(cuò)了嗎,為什么catch(err)啊
回答
編輯回答
鹿惑

axios攔截器里面,設(shè)置content-type了嗎?
后臺(tái)接收content-type:application/json;還是application/x-www-form-urlencoded或者別的,
400后臺(tái)應(yīng)該是都沒(méi)收到你的請(qǐng)求

2017年9月11日 18:05
編輯回答
奧特蛋

post 的data 要 JSON.stringify(bodyData)

2018年6月30日 06:31
編輯回答
她愚我

樓上說(shuō)的很對(duì),主要看后臺(tái)要求post請(qǐng)求可以接受什么數(shù)據(jù),axios中post默認(rèn)請(qǐng)求數(shù)據(jù)格式是application/json,如果后臺(tái)不支持可以
設(shè)置請(qǐng)求頭格式
在config配置headers: {'X-Requested-With':'XMLHttpRequest'},
格式化數(shù)據(jù)
并引入axios里面內(nèi)置的qs,不需要重新下載

import qs from 'qs'
this.axios({
      method: 'POST',
      url: URL,
      data: qs.stringify({
        "mobile": tel,
        "auth_code": code,
        "lngLat": this.lngLat,
        "regSource": 'H5',
        "frc": "H5",
        "ref": this.ref
      }),
      headers: {
        'appId': config.config.APPID,
        'token': this.token,
        'X-Requested-With':'XMLHttpRequest'
      }
    })
2017年4月10日 04:44
編輯回答
青黛色

看你后臺(tái)需要接受什么格式的數(shù)據(jù)

2018年5月20日 20:24
編輯回答
浪蕩不羈

語(yǔ)法格式錯(cuò)誤,估計(jì)傳的參數(shù)和服務(wù)端要求的不一致造成的。

2017年7月24日 18:06