使用 axios 和 nuxt 发出 GET 请求时如何获取响应标头?

时间:2021-01-12 09:58:34

标签: axios nuxt.js

我正在尝试从我的后端获取 CSRF 令牌。使用 fetch 时,我可以简单地

                const response = await fetch(`/account/csrf/`, {
                    credentials: "include",
                });
                
                const data = await response
                let csrfToken = data.headers.get("X-CSRFToken"); // how is this done with axios?

我试过了

const csrfToken = await this.$axios.$get(`/account/csrf/`, {
 /*
 what is the equivalent here? 
 I don't wish to *send* anything to the server, but rather retrieve, so I guess I can't use headers {}
 */
});

1 个答案:

答案 0 :(得分:1)

您可以使用来自 Axios 的原始 $get() 方法,而不是使用来自 nuxt 模块的 get() 助手获取,如下所示:

const { data, headers } = await this.$axios.get('/account/csrf/', {
  withCredentials: true
});

const csrfToken = headers["X-CSRFToken"];
相关问题