reactjs - django 和 axios:如何为 OPTIONS 启用 CORS
问题描述
我正在使用 axiosPOST
在我的 reactjs 页面中执行请求。
const url = "test/";
const data = {
email: "test@test.com",
password: "test",
};
const headers = {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
};
axios.post(url, data, headers)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
在我的 Django 中,我有
CORS_ALLOW_ALL_ORIGINS = True
我的 axios 请求工作正常。
但是现在在我的 Django 视图(对应于 url)中,我故意放了一个错误的 python 语句。
IE
def test(request):
a+=1
..... return some json
**如果我不放 a+=1 那么 axios 工作正常,没有任何错误
当运行 axios 代码时,我得到
并且
在我的 Django 控制台中,我看到:
Traceback (most recent call last):
.........
........../views.py", line 139, in test
a+=1
NameError: name 'a' is not defined
但一般来说,如果我不使用 rest api,当浏览器本身发生任何错误时,Django 会显示回溯 html。
即使使用 ajax,我也可以在 developertools 中看到响应。
但是使用 axios 它没有显示
我检查了邮递员它显示了回溯
有人可以帮助如何实现这一目标。我也想在脚本中捕获回溯
解决方案
推荐阅读
- sql - 按日期建立日期参数:小时而不是日期
- class - 如何从飞镖颤振中的单个泛型类返回多种类类型?
- postgresql - 将 postgresql 从版本 11 升级到 12 会导致“您的安装引用了丢失的可加载库”
- python - 根据熊猫数据框中的特定ID更改列的值
- wordpress - Htaccess 将 Modula Gallery 图像文件名重写为 LiteSpeed 缓存图像
- javascript - SignalR 锁定按钮
- reactjs - 开玩笑和酶通过了个人测试,但未能全部运行
- python - 如何根据条件在 panadas 中删除行?
- google-sheets - 如何删除单元格中心的文本
- java - 如何在java中平均显示一行数字