axios catch错误

    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)
    
    
    这样写哪里错了吗,为什么catch(err)啊
阅读 5.9k
5 个回答

post 的data 要 JSON.stringify(bodyData)

楼上说的很对,主要看后台要求post请求可以接受什么数据,axios中post默认请求数据格式是application/json,如果后台不支持可以
设置请求头格式
在config配置headers: {'X-Requested-With':'XMLHttpRequest'},
格式化数据
并引入axios里面内置的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'
      }
    })

语法格式错误,估计传的参数和服务端要求的不一致造成的。

看你后台需要接受什么格式的数据

axios拦截器里面,设置content-type了吗?
后台接收content-type:application/json;还是application/x-www-form-urlencoded或者别的,
400后台应该是都没收到你的请求

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题