In cross-domain requests, the browser does not automatically carry the cookie of the cross-domain request by default. This is due to security restrictions caused by the browser’s homology policy (Same-Origin Policy). However, some options may be set to allow the browser to carry the cookie when sending a cross-domain request.
In Axios, you can automatically carry cookie by setting the withCredentials
option to true
in the request configuration.
const axios = require('axios');
axios.get('https://example.com/api/*', {
withCredentials: true
})
.then(response => {
// Handle response
console.log(response.data);
})
.catch(error => {
console.error('Request failed', error);
});
withCredentials: true
tells the browser to carry cookie in a cross-domain request. Note, however, that the server also needs to be configured to allow cross-domain requests to carry cookie.
Access-Control-Allow-Credentials: true
This document is transferred from https://blog.csdn.net/weixin_42414313/article/details/136058675,If there is any infringement,Please contact to delete。