javascript - 检查 Axios Promise catch 块 VueJs 中的 HTTP 状态码
问题描述
VueJS 的新手。我有以下代码使用 axios 从控制器中检索数据:
SubmitForm: function () {
axios({
method: 'post',
url: '/Home/SubmitedForm',
data: { "Fields": this.$data }
}).then(res => {
alert('Successfully submitted the form ');
window.close();
}).catch(err => {
if (err.response.status == 409) {
alert(`Already exists. See details: ${err}`)
}
else {
alert(`There was an error submitting your form. See details: ${err}`)
}
});
当 Controller 方法 SubmittedForm 返回 409 时,我想抛出一个特定的警报,否则只抛出一个通用的警报。基于这个页面:https ://gist.github.com/fgilio/230ccd514e9381fafa51608fcf137253我写了上面的代码。但是,即使返回的 http 状态是 409,它仍然显示通用警报。我很确定我在这里缺少一些理解。有人可以帮我找出我在这里做错了什么吗?在 localhost 上按预期工作,但在 azurewebsites 上发布后,它再次显示一般错误。
解决方案
也许您的 API 端点会阻止 CORS 策略,然后您无法读取此类错误的状态(尽管它在开发工具的“网络”选项卡中可见)。
您可以安装一个浏览器扩展程序,如“CORS 无处不在”来测试它是否可以正常工作,但是默认情况下,任何被 CORS 阻止的 API 调用都会在浏览器的控制台中显示警告/错误。
推荐阅读
- javascript - 使用 JavaScript 解压缩 zip 文件
- java - Spring Security:提取 oidc 角色声明给 Spring 当局
- batch-file - 将批处理脚本的输出重定向到日志文件
- javascript - 如何以编程方式检索谷歌应用程序脚本库版本号和注释?
- spring-boot - 如何在 netty 中提供一个全信任的 SslProvider?
- filter - 设置过滤方法
- nsis - 使用 NSIS,有没有办法确定 TextBox 只能包含英文和数字?
- javascript - 如何检测窗口是否被js打开
- php - Wordpress 自定义插件错误 - 警告:未定义的数组键“home_team”
- python - Imageai 检测器 RuntimeWarning