From e8ab82c2c103aa3a45712fb2794050b5b32b4140 Mon Sep 17 00:00:00 2001 From: CNine Date: Mon, 15 Oct 2018 13:13:21 +0800 Subject: [PATCH] fix[MockJS]: fix bug with withCredentials after using mockjs (#1194) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复 Mock 导致请求丢失 Cookie 的问题 修复 Mock 导致 Cookie 丢失的问题,只有在 XHR.open() 周期时,自定义的 withCredentials 会被挂载,此时检查是否是未被拦截的 xhr,并挂载自定义的 withCredentials ,无则默认为 false --- src/mock/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/mock/index.js b/src/mock/index.js index e6644ae1..3e00e918 100644 --- a/src/mock/index.js +++ b/src/mock/index.js @@ -4,6 +4,16 @@ import articleAPI from './article' import remoteSearchAPI from './remoteSearch' import transactionAPI from './transaction' +// 修复在使用 MockJS 情况下,设置 withCredentials = true,且未被拦截的跨域请求丢失 Cookies 的问题 +// https://github.com/nuysoft/Mock/issues/300 +Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send +Mock.XHR.prototype.send = function() { + if (this.custom.xhr) { + this.custom.xhr.withCredentials = this.withCredentials || false + } + this.proxy_send(...arguments) +} + // Mock.setup({ // timeout: '350-600' // })