axios cookie
时间: 2023-09-05 17:13:04 浏览: 33
Axios is a popular JavaScript library used for making HTTP requests. It does not include built-in support for cookies, but it is possible to use Axios to send and receive cookies using the `withCredentials` option.
To send cookies with Axios, set the `withCredentials` option to `true` when making the request:
```
axios.get('https://example.com/api/data', { withCredentials: true })
.then(response => {
// handle response
});
```
This will send any cookies associated with the current domain and path with the request.
To receive cookies with Axios, the server must set the `Access-Control-Allow-Credentials` header to `true` in the response. This is required for cross-origin requests when using cookies.
```
app.use(cors({
origin: 'https://example.com',
credentials: true
}));
```
By setting the `credentials` option to `true`, the server will respond with the `Access-Control-Allow-Credentials` header set to `true`.
Once the server is configured to allow credentials, Axios will automatically handle setting and receiving cookies.