javascript - 使用 Axios 存储 cookie 连接
问题描述
我想用axios访问一个api的信息,问题是axios无法获取数据。我已经用 Postman 测试过,我可以很好地访问数据。我还注意到 Posteman 存储了一个登录 cookie,我试图用 Axios 重现,但我不能。
我看到axios.defaults.withCredentials = true
必须使用它,但它不起作用,也许我做错了。
这是我的代码:
getData() {
axios.defaults.withCredentials = true;
let url = "***"
axios
.get(url)
.then(response => {
this.name = response
}).catch((error) => {
this.name = error;
})
}
先感谢您!
解决方案
如果您自己没有设置邮递员 cookie,那么它就是邮递员的东西,因此它可能对您的 axios 请求没有用处
小心,axios 返回的数据就像{"headers": stuff, "data": yourdata}
Postman 只显示“数据”键中的数据一样
所以你可能想做
getData() {
let url = "***"
axios
.get(url)
.then(response => {
this.name = response.data
}).catch((error) => {
this.name = error;
})
}
否则,您可以将您的 Postman 请求导出到代码,这样您就可以看到 Postman 发送到 API 的内容(使用 Postman 中的“保存”按钮下的“代码”按钮)
推荐阅读
- php - 集合在 first() 方法上不返回对象
- jquery - 响应式引导轮播,每张幻灯片有两个元素
- php - 为什么 CookieJar 在两个带有 Behatch 扩展的请求之间被清空?
- kubernetes - 在 Alpine Linux armhf 上安装 Kubernetes
- java - 在邮件正文属性文件中添加图像
- android - 以编程方式获取服务代码以用于服务调用连接?
- .net - 如何使用 C# 获取 Azure 服务主体的不记名令牌
- single-sign-on - OIDC AspNetCore OpenIdConnectHandler 相关性失败状态属性未找到
- regex - Perl 正则表达式:负前瞻
- java - 使用 jooq 将 COUNT 值除以另一个值时的舍入问题