axios - 使用 axios 和 nuxt 发出 GET 请求时如何获取响应标头?
问题描述
我正在尝试从我的后端获取 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 {}
*/
});
解决方案
$get()
您可以使用 Axios 中的 raw 方法,而不是从 nuxt 模块中获取帮助程序,get()
如下所示:
const { data, headers } = await this.$axios.get('/account/csrf/', {
withCredentials: true
});
const csrfToken = headers["X-CSRFToken"];
推荐阅读
- android - 如何在颤动中获取临时存储目录?
- reactjs - 在 React js 中导航页面的最佳方法
- google-apps-script - 将 Gmail 标签导出到 Google 表格
- html - 试图制作一个垂直截断子元素的滚动框
- ffmpeg - 捕获多个流和远程流的动态帧并将其编码给并发用户的最佳方法
- c++ - 调用 `string::c_str()` 时实际做了什么?
- chromecast - 在 exoplayer 中初始化 CastContext 失败
- reactjs - 获取和组织数据并呈现响应
- android - 蓝牙信标 - Azure IoT 中心 - Android 应用
- java - 在 M1 Mac 上使用 Spring Boot 运行深入的 Vaadin Flow 教程(ARM AArch64 与 Intel x86 芯片)