From 0c7dd680c734865e893e882099d43645885701a6 Mon Sep 17 00:00:00 2001 From: CNine Date: Sat, 13 Oct 2018 14:12:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Mock=20=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=B8=A2=E5=A4=B1=20Cookie=20=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复 Mock 导致 Cookie 丢失的问题,只有在 XHR.open() 周期时,自定义的 withCredentials 会被挂载,此时检查是否是未被拦截的 xhr,并挂载自定义的 withCredentials ,无则默认为 false --- src/mock/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/mock/index.js b/src/mock/index.js index e6644ae1..414a2563 100644 --- a/src/mock/index.js +++ b/src/mock/index.js @@ -4,6 +4,15 @@ import articleAPI from './article' import remoteSearchAPI from './remoteSearch' import transactionAPI from './transaction' +// * 修复 Mock 情况下,设置 withCredentials = true,且未被拦截的跨域请求丢失 Cookies 的问题 +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' // })