javascript - 在 axios 中响应代码 400 时出现问题
问题描述
我正在使用 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 的脚本文件。得到同样的错误。
解决方案
预检请求不应响应 400 错误。
推荐阅读
- python - KMeans 聚类不适用于超过 4 列的数据框
- android - onCharacteristicChanged 没有被击中。安卓低功耗蓝牙
- angular - 仅限 Cognito 身份验证用户的 S3 访问
- jquery - Django 删除函数出现错误(字段 id 需要一个数字但得到了)
- sql - 将表变量中的值分配给表的每一行?
- javascript - JS调用多个存储函数,从this开始
- python - 用于单元测试的python中的模拟流API
- dotnetnuke - 如何从 2sxc 模块中的另一个门户访问某些实体关联文件?
- node.js - Socket.io 客户端:总是无效的命名空间消息
- azure - Azure RBAC 应用程序洞察组件贡献者与监控贡献者