【问题标题】:Get header response Nuxt Axios获取标头响应 Nuxt Axios
【发布时间】:2019-09-07 23:19:07
【问题描述】:

我对 nuxt 覆盖 axios 响应有疑问。 首先,我在空的 nodejs 项目中尝试使用 axios 来检查资源是否可用。

axios
    .head(
        'https://needtocheckthislink'
    )
    .then(function(response) {
// here code 200
        console.log(response.status);
    })
    .catch(function(error) {
// Some errors 403 / 404
        console.log(error.response.status);
    });

它工作正常,但在 nuxt 我无法从我的请求中获取 response.data。 Nuxt 提供错误代码,但我无法解释。

// for 403
Error: "Network Error"
    NuxtJS 2

    createError

    handleError

// for 404
Error: "Request failed with status code 404"
    NuxtJS 3

    createError

    settle

    handleLoad

我怎样才能正确处理这个请求?

【问题讨论】:

标签: node.js axios nuxt.js


【解决方案1】:

因为我不确定这个解决方案是否在客户端:Axios 我已经使用了基本的 http 标头请求。

let client = new XMLHttpRequest();
client.open("GET", "http://testUrl", true);
client.send();
client.onreadystatechange = function() {
   if(this.readyState == this.HEADERS_RECEIVED) {
      if(client.status === 200){
         console.log("res 200");
      }
      else if(client.status === 404){
         console.log("err 404");
      }
      else if(client.status === 0){
         console.log("forbiden")
      }
      client.abort();
   }
}

【讨论】:

    猜你喜欢
    • 2020-08-10
    • 2022-01-12
    • 2019-11-26
    • 2017-03-28
    • 1970-01-01
    • 2022-12-29
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多