VueJS HTTP请求错误500处理

编程入门 行业动态 更新时间:2024-10-22 15:36:37
本文介绍了VueJS HTTP请求错误500处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到这个问题,我向API发出HTTP请求,如果出现错误(特别是错误500),JS就会中断或进入无限循环,我应该关闭窗口并重新打开页面。我需要的是一个弹出的消息,并以非常通用的方式解释发生了什么。我该怎么处理这种错误呢?

I am having this problem where I make HTTP Request to the API and in case of error ( specially error 500) the JS just breaks or goes into infinite loop and I should close the window and re-open the page. What I need is a message to pop up and in very generic way explain what happened. How should I handle this kind of error any ideas?

示例请求:

this.$http.get('people', { params }).then(({ data }) => { this.setFetching({ fetching: false }) })

推荐答案

然后 接受第二次回调以处理错误。除了 .then 之外,您还可以提供 .catch 来处理更严重的故障。

then accepts a second callback to handle errors. You can also supply a .catch in addition to a .then to handle more severe failures.

new Vue({ el: '#app', data: { fetching: true }, mounted() { this.$http.get('people') .then(() => { this.setFetching({ fetching: false }) }, (err) => { console.log("Err", err); }) .catch((e) => { console.log("Caught", e); }) } });

<script src="//cdnjs.cloudflare/ajax/libs/vue/2.2.6/vue.min.js"></script> <script src="cdn.jsdelivr/vue.resource/1.2.1/vue-resource.min.js"></script> <div id="app"> </div>

更多推荐

VueJS HTTP请求错误500处理

本文发布于:2023-11-16 11:17:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   VueJS   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!