Axios Stop 404 错误出现在console.log

JaC*_*hNo 9 javascript vue.js axios

我正在通过 Axios 检查资源是否存在,如果资源不存在,期望返回 404 是正常的。

但是,当返回 404 时,它会显示在控制台中。我试图捕捉错误,但这并不能阻止它被 chrome 显示。

        axios.get('/api/user/checkin').then((response) => {
            Vue.set(UserStore,'checkin', response.data)
            this.loading = false;
        }).catch((error) =>{})
Run Code Online (Sandbox Code Playgroud)

我正在检查用户是否被选中,如果他们被选中,我将返回他们签入时间的记录以及网站上的一些详细信息。

如果它们不是,那么我没有什么可返回的,所以我返回 404。我可以返回一个空白记录,但这确实让我感到很平静。

Ahm*_*ah. 5

这是chrome 的行为。看看这个还有这个
您可以按照此答案的建议进行catchconsole.clear()操作。

axios.get('/api/user/checkin')
  .then((response) => {
    Vue.set(UserStore,'checkin', response.data)
    this.loading = false;
  })
  .catch(() => console.clear()) // this will clear any console errors caused by this request
Run Code Online (Sandbox Code Playgroud)


.catch(console.clear)简称。

但请注意,您将丢失以前的所有控制台日志

编辑:

您可能只想在收到 404 响应时清除控制台。

axios.get('/api/user/checkin')
  .then((response) => {
    Vue.set(UserStore,'checkin', response.data)
    this.loading = false;
  })
  .catch(() => console.clear()) // this will clear any console errors caused by this request
Run Code Online (Sandbox Code Playgroud)

有关更多信息, 请参阅处理 Axios 中的错误。