Axios + CORS cross domain requests carry cookies


Axios + CORS cross domain requests carry cookies

Solution: CORS

front end

axios.interceptors.request.use(config => {
  config.withCredentials = true;
  return config;

Back end configuration response header

"Access-Control-Allow-Origin": Request Headers Origin
"Access-Control-Allow-Credentials": true

After setting, cross domain request carrying can be realizedCookieYes.

Abnormal situation

After setting the above steps, set theRequest HeadersStill not carrying itCookie

(function(open) { = function(method, url, async, user, pass) {
      function() {
        console.log(this); // MockXMLHttpRequest
    );, method, url, async, user, pass);

it turns out to be the case thatMockJsRevisedXMLHttpRequest

// mock.js
if (XHR) window.XMLHttpRequest = XHR

Manual settingwithCredentials

Mock.XHR.prototype.withCredentials = true;

Problem solving.