简体   繁体   English

检查 Axios promise 捕获块 VueJs 中的 HTTP 状态代码

[英]Check HTTP status code in the Axios promise catch block VueJs

New to VueJS. VueJS 的新手。 I have the following code that retrieves data from the Controller using axios:我有以下代码使用 axios 从 Controller 检索数据:

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}`)
        }

    });

When the Controller method SubmittedForm returns 409, I want to throw a specific alert else just throw a generic alert.当 Controller 方法 SubmittedForm 返回 409 时,我想抛出一个特定的警报,否则只抛出一个通用的警报。 Based on this page:https://gist.github.com/fgilio/230ccd514e9381fafa51608fcf137253 I wrote the above code.基于此页面:https://gist.github.com/fgilio/230ccd514e9381fafa51608fcf137253我写了上面的代码。 However, even thought the http status returned is 409, it still show the generic alert.但是,即使返回的 http 状态为 409,它仍然显示一般警报。 I'm pretty sure I'm missing some understanding here.我很确定我在这里缺少一些理解。 Can someone please help me find out what am I doing wrong here?有人可以帮我找出我在这里做错了什么吗? Works as expected on localhost but after publishing on azurewebsites it again displays the generic error.在 localhost 上按预期工作,但在 azurewebsites 上发布后,它再次显示一般错误。

Maybe your API endpoint brakes CORS policy, you can't read status of such error then (despite the fact that it is visible in Networks tab in dev tools).也许您的 API 端点制动 CORS 策略,然后您无法读取此类错误的状态(尽管它在开发工具的“网络”选项卡中可见)。

You can install a browser extension like "CORS everywhere" to test if it works then, but any call to API blocked by CORS will show a warning/error in the browser's console by default.你可以安装一个浏览器扩展,比如“CORS无处不在”来测试它是否可以工作,但是任何被 CORS 阻止的对 API 的调用都会默认在浏览器的控制台中显示警告/错误。

Probably because err.response.status is a string and you are comparing to number可能是因为 err.response.status 是一个字符串,而您正在与数字进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 axios 时,catch 块中的状态代码 300 返回错误 - Status code 300 return error in catch block when using axios axios如何在.catch()中获取状态代码? - How can axios get the status code in .catch()? 如果使用较短的语法,Promise(axios 请求)在 catch 块中被记录为挂起 - Promise (axios request) is logged as pending in catch block if shorter syntax is used Axios:哪些 HTTP 响应状态代码导致 then() 哪些导致 catch()? - Axios: which HTTP response status codes result in then() and which in catch()? VueJS、expressJS、axios 删除错误:请求失败,状态码为 404 - VueJS, expressJS, axios DELETE error: Request failed with status code 404 在http.get catch块中拒绝Promise - Reject a promise within an http.get catch block 被拒绝的 HTTP 承诺没有命中 catch 块 - rejected HTTP promise doesn't hit catch block Axios - 未捕获(承诺)错误:请求失败,状态码为 500 - Axios - Uncaught (in promise) Error: Request failed with status code 500 Axios - [未处理的承诺拒绝:错误:请求失败,状态码为 404] - Axios - [Unhandled promise rejection: Error: Request failed with status code 404] 处理状态码错误 - 尝试在死连接上捕获 Axios - Handling status code error - try catch Axios on dead connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM