From b91477cca937e74622982c410f79ae474c6c3d92 Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 9 Jun 2022 17:35:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwindow.fetch=E7=9A=84?= =?UTF-8?q?=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/next.js | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 9f02a26..3bde410 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bytedo/fetch", - "version": "2.0.0", + "version": "2.0.1", "description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch", "main": "dist/index.js", "files": [ diff --git a/src/next.js b/src/next.js index 63a3659..24a5d61 100644 --- a/src/next.js +++ b/src/next.js @@ -77,7 +77,6 @@ class _Request { __next__() { var options = this.options - var params = null var hasAttach = false // 是否有附件 var crossDomain = false // 是否跨域 var noBody = NOBODY_METHODS.includes(options.method) @@ -89,23 +88,22 @@ class _Request { case 'number': case 'string': this.__type__('text') - params = options.body break case 'object': // 解析表单DOM if (options.body.nodeName === 'FORM') { options.method = options.body.method.toUpperCase() || 'POST' - params = Format.parseForm(options.body) - hasAttach = params.constructor === FormData + options.body = Format.parseForm(options.body) + hasAttach = options.body.constructor === FormData // 如果是一个 FormData对象,且为不允许携带body的方法,则直接改为POST } else if (options.body.constructor === FormData) { hasAttach = true + // 修正请求类型 if (noBody) { options.method = 'POST' } - params = options.body } else { for (let k in options.body) { if (toS.call(options.body[k]) === '[object File]') { @@ -118,11 +116,10 @@ class _Request { if (noBody) { options.method = 'POST' } - params = Format.mkFormData(options.body) - } else { - params = options.body + options.body = Format.mkFormData(options.body) } } + break } } if (hasAttach) { @@ -146,16 +143,17 @@ class _Request { // 拼接到url上 if (noBody) { - params = Format.param(params) - if (params) { - options.url += (~options.url.indexOf('?') ? '&' : '?') + params + let tmp = Format.param(options.body) + if (tmp) { + options.url += (~options.url.indexOf('?') ? '&' : '?') + tmp } + delete options.body } else { if (!hasAttach) { if (~options.headers['content-type'].indexOf('json')) { - params = JSON.stringify(params) + options.body = JSON.stringify(options.body) } else { - params = Format.param(params) + options.body = Format.param(options.body) } } }