camsong / fetch-ie8

A window.fetch JavaScript polyfill supporting IE8
MIT License
279 stars 33 forks source link

empty plain object cause "unsupported BodyInit type" error #9

Open mingzepeng opened 7 years ago

mingzepeng commented 7 years ago
fetch(url,{
    body : {},
    method : 'POST'
})

this code may cause "unsupported BodyInit type" error because of below code from fetch-ie8 source

this._initBody = function(body, options) {
      this._bodyInit = body
      if (typeof body === 'string') {
        this._bodyText = body
      } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
        this._bodyBlob = body
        this._options = options
      } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
        this._bodyFormData = body
      } else if (!body) {
        this._bodyText = ''
      } else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
        // Only support ArrayBuffers for POST method.
        // Receiving ArrayBuffers happens via Blobs, instead.
      } else {
        throw new Error('unsupported BodyInit type')
      }
    }
camsong commented 7 years ago

Fetch body does not support object. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body

mingzepeng commented 7 years ago

抱歉,我没有表述清楚我的意思,空对象是不被支持的,但你的库则会直接报错, 而不是被忽略 在 win10 chrome 58.0.3029.81 和 firefox 53 上

fetch('./',{method : 'POST',body:{}})

上述代码仍然会产生请求,只不过 body 部分被忽略了,fetch 请求仍然会正常进行。