Axios 是否支持 Set-Cookie?是否可以通过 Axios HTTP 请求进行身份验证? [英] Does Axios support Set-Cookie? Is it possible to authenticate through Axios HTTP request?

查看:37
本文介绍了Axios 是否支持 Set-Cookie?是否可以通过 Axios HTTP 请求进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Axios HTTP 请求调用对快速 API 后端进行身份验证.我能够在响应标头中看到Set-Cookie",但未设置 cookie.是否可以通过 Axios HTTP 调用设置 cookie?

I'm trying to authenticate express API back-end using Axios HTTP request call. I was able to see 'Set-Cookie' in the response header, but cookie was not set. Is it possible to set cookies through Axios HTTP calls?

Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 355
Content-Type: application/json; charset=utf-8
Date: Fri, 28 Sep 2018 05:59:01 GMT
ETag: W/"163-PAMc87SVHWkdimTJca7oRw"
Set-Cookie: token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...; Max-Age=3.6; Path=/; Expires=Fri, 28 Sep 2018 05:59:04 GMT; HttpOnly
X-Powered-By: Express

推荐答案

试试这个!

axios.get('your_url', {withCredentials: true}); //for GET
axios.post('your_url', data, {withCredentials: true}); //for POST
axios.put('your_url', data, {withCredentials: true}); //for PUT
axios.delete('your_url', data, {withCredentials: true}); //for DELETE

有关来自 axios 文档的更多信息:

For more information on this from the axios docs:

withCredentials 指示是否应使用凭据进行跨站点访问控制请求";- https://github.com/axios/axios

"withCredentials indicates whether or not cross-site Access-Control requests should be made using credentials" - https://github.com/axios/axios

有关 withCredentials 的更多详细信息:

More detail on withCredentials:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials

这篇关于Axios 是否支持 Set-Cookie?是否可以通过 Axios HTTP 请求进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆