From 31d56143f5c2570c697f664375bf7149c9278396 Mon Sep 17 00:00:00 2001 From: liyang <1711467488@qq.com> Date: Sat, 13 Oct 2018 20:48:33 +0800 Subject: [PATCH] no message Former-commit-id: 4f114549b4f048298ef6d2bd37b7cb57ca4cecca --- src/mock/index.js | 5 +++++ src/mock/plugin/mock-cookie/index.js | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 src/mock/plugin/mock-cookie/index.js diff --git a/src/mock/index.js b/src/mock/index.js index 86af00f..f266338 100644 --- a/src/mock/index.js +++ b/src/mock/index.js @@ -1,5 +1,10 @@ import Mock from 'mockjs' +// 补丁 解决 mock.js 影响 Cookie 携带 +import MockCookie from './plugin/mock-cookie' + +MockCookie(Mock) + // 导入所有的接口 const req = context => context.keys().map(context) req(require.context('./api/', true, /\.js$/)) diff --git a/src/mock/plugin/mock-cookie/index.js b/src/mock/plugin/mock-cookie/index.js new file mode 100644 index 0000000..0c10128 --- /dev/null +++ b/src/mock/plugin/mock-cookie/index.js @@ -0,0 +1,10 @@ +export default function (Mock) { + // 解决 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) + } +}