修复window.fetch的请求

master
yutent 2022-06-09 17:35:34 +08:00
parent ba4717cd8c
commit b91477cca9
2 changed files with 12 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@bytedo/fetch", "name": "@bytedo/fetch",
"version": "2.0.0", "version": "2.0.1",
"description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch", "description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch",
"main": "dist/index.js", "main": "dist/index.js",
"files": [ "files": [

View File

@ -77,7 +77,6 @@ class _Request {
__next__() { __next__() {
var options = this.options var options = this.options
var params = null
var hasAttach = false // 是否有附件 var hasAttach = false // 是否有附件
var crossDomain = false // 是否跨域 var crossDomain = false // 是否跨域
var noBody = NOBODY_METHODS.includes(options.method) var noBody = NOBODY_METHODS.includes(options.method)
@ -89,23 +88,22 @@ class _Request {
case 'number': case 'number':
case 'string': case 'string':
this.__type__('text') this.__type__('text')
params = options.body
break break
case 'object': case 'object':
// 解析表单DOM // 解析表单DOM
if (options.body.nodeName === 'FORM') { if (options.body.nodeName === 'FORM') {
options.method = options.body.method.toUpperCase() || 'POST' options.method = options.body.method.toUpperCase() || 'POST'
params = Format.parseForm(options.body) options.body = Format.parseForm(options.body)
hasAttach = params.constructor === FormData hasAttach = options.body.constructor === FormData
// 如果是一个 FormData对象,且为不允许携带body的方法,则直接改为POST // 如果是一个 FormData对象,且为不允许携带body的方法,则直接改为POST
} else if (options.body.constructor === FormData) { } else if (options.body.constructor === FormData) {
hasAttach = true hasAttach = true
// 修正请求类型
if (noBody) { if (noBody) {
options.method = 'POST' options.method = 'POST'
} }
params = options.body
} else { } else {
for (let k in options.body) { for (let k in options.body) {
if (toS.call(options.body[k]) === '[object File]') { if (toS.call(options.body[k]) === '[object File]') {
@ -118,11 +116,10 @@ class _Request {
if (noBody) { if (noBody) {
options.method = 'POST' options.method = 'POST'
} }
params = Format.mkFormData(options.body) options.body = Format.mkFormData(options.body)
} else {
params = options.body
} }
} }
break
} }
} }
if (hasAttach) { if (hasAttach) {
@ -146,16 +143,17 @@ class _Request {
// 拼接到url上 // 拼接到url上
if (noBody) { if (noBody) {
params = Format.param(params) let tmp = Format.param(options.body)
if (params) { if (tmp) {
options.url += (~options.url.indexOf('?') ? '&' : '?') + params options.url += (~options.url.indexOf('?') ? '&' : '?') + tmp
} }
delete options.body
} else { } else {
if (!hasAttach) { if (!hasAttach) {
if (~options.headers['content-type'].indexOf('json')) { if (~options.headers['content-type'].indexOf('json')) {
params = JSON.stringify(params) options.body = JSON.stringify(options.body)
} else { } else {
params = Format.param(params) options.body = Format.param(options.body)
} }
} }
} }