修复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",
"version": "2.0.0",
"version": "2.0.1",
"description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch",
"main": "dist/index.js",
"files": [

View File

@ -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)
}
}
}