Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

XMLHttpRequest polyfill issue #106

Closed
wkh237 opened this issue Aug 26, 2016 · 2 comments
Closed

XMLHttpRequest polyfill issue #106

wkh237 opened this issue Aug 26, 2016 · 2 comments

Comments

@wkh237
Copy link
Owner

wkh237 commented Aug 26, 2016

Derived #69

@wkh237
Copy link
Owner Author

wkh237 commented Aug 26, 2016

@kdedakia, after the investigation, I've located a problem in our XMLHttpRequest implementation, when response header contains Content-Type : application/json, the xhr.response propertywill be converted to object (which simply JSON.parse xhr.responseText), this is based on my understanding to W3C standard.

However, the WHATWG-Fetch implementation and those ones in many browsers seems not convert xhr.response to JSON object when response header has the application/json content type, and this is why fetch does not working after replacing the XMLHttpRequest. The following code snippet is the body parser of WHATWG-Fetch

function Body() {
    this.bodyUsed = false

    this._initBody = function(body) {
      this._bodyInit = body
      if (typeof body === 'string') {
        this._bodyText = body
      } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
        this._bodyBlob = body
      } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
        this._bodyFormData = body
      } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
        this._bodyText = body.toString()
      } 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')
      }

Since it does not ever expect xhr.response would be an object, it always throws error. I've change our implementation to fit the spec. You can try install the beta version and see if that works properly.

$ rnpm install --save react-native-fetch-blob@beta

However, when the response is binary data it may not working properly, in this case, you can use our network APIs as an alternative 😏

@mlumbroso
Copy link

Working great for me now !

@wkh237 wkh237 closed this as completed Sep 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants