我正在尝试使用 Axios HTTP 请求调用来验证 Express API 后端。 我能够在响应 header 中看到“Set-Cookie”,但未设置 cookie。是否可以通过axios HTTP调用设置cookie?
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 文档的更多信息:
“withCredentials 指示是否应使用凭据发出跨站点访问控制请求”- https://github.com/axios/axios
有关 withCredentials 的更多详细信息:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
关于express - axios支持Set-Cookie吗?是否可以通过axios HTTP请求进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52549079/