问题描述
我正在尝试使用Axios HTTP请求调用对Express 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
有关凭证的更多详细信息:
More detail on withCredentials:
https://developer.mozilla.org/zh- US/docs/Web/API/XMLHttpRequest/withCredentials
这篇关于Axios是否支持Set-Cookie?是否可以通过Axios HTTP请求进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!