在axios中使用代码400进行响应时出错了

时间:2018-05-02 11:17:30

标签: javascript vue.js cors axios

我正在使用Vue和Axios构建SPA前端。当服务器在CROS中发送包含代码400的响应时。浏览器的控制台日志:

(2) POST http://dev.sportx.one/api/token/ 400 (Bad Request) **This line logged twice**
Failed to load http://dev.sportx.one/api/token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.
Error: Network Error
    at e.exports (spread.js:25)
    at XMLHttpRequest.l.onerror (spread.js:25)

我如何发送请求:

api.request({
 url: '/api/token/',
  method: 'post',
  data: {
    email: '972372527@qq.com',
    password: 'aaaaaaaA'
  }
}).catch(err=>{console.log(err)})

浏览器的网络检查器显示已发送2个请求(OPTIONS和POST)。他们两个都按预期工作。

如果服务器发送20X响应而没有任何错误捕获,该怎么办?一切都很好。

我在新的html文件中尝试相同的代码,只加载axios的脚本文件。得到了同样的错误。

2 个答案:

答案 0 :(得分:0)

您需要设置'Access-Control-Allow-Origin':'*'

答案 1 :(得分:0)

Preflight请求不应响应400错误。