XHR throws an exception accessing responseText, if responseType === "arraybuffer" #2498

Closed
metadings opened this Issue Jul 24, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@metadings

If you try using xhr.responseType = "arraybuffer" (jqXHR.xhrFields = { responseType: "arraybuffer" }), the code issues an exception InvalidStateError (in Firefox, when accessing responseText, instead of response):

// Support: IE9
// Accessing binary-data responseText throws an exception
// (#11426)
typeof xhr.responseText === "string" ? {
    text: xhr.responseText
} : undefined

I changed jquery-1.6.1; now I'm going to work with a current version to make a PR or does anyone have something laying around...?

@metadings

This comment has been minimized.

Show comment
Hide comment
@metadings

metadings Jul 24, 2015

Probably I need to go back to a try...catch block around responseText, responseXml and response, so I'm just getting the whole response and then sorting out...

Probably I need to go back to a try...catch block around responseText, responseXml and response, so I'm just getting the whole response and then sorting out...

@dmethvin

This comment has been minimized.

Show comment
Hide comment
@dmethvin

dmethvin Jul 25, 2015

Member

It seems like this is subverting the normal $.ajax processing model and confusing the code, as far as it's concerned this is still a request where it should look at responseText. I am wondering if defining a converter for an "arraybuffer" dataType would be the appropriate way to do this. Can you provide a complete example?

Member

dmethvin commented Jul 25, 2015

It seems like this is subverting the normal $.ajax processing model and confusing the code, as far as it's concerned this is still a request where it should look at responseText. I am wondering if defining a converter for an "arraybuffer" dataType would be the appropriate way to do this. Can you provide a complete example?

@metadings

This comment has been minimized.

Show comment
Hide comment
@metadings

metadings Jul 25, 2015

I defined it using

// Install plain dataType
jQuery.ajaxSetup({
    accepts: {
        plain: "text/plain"
    },
    responseFields: {
        plain: "response"
    }
    /* , converters: {
        "* plain": function (arrayBuffer) {
            if (arrayBuffer) {
                return new Uint8Array(arrayBuffer);
            }
        }
    } /**/
});

// Handle cache's special case and global
jQuery.ajaxPrefilter("plain", function ( s, originalSettings, jqXHR ) {
    s.xhrFields = {
        responseType: "arraybuffer"
    };
    // s.dataTypes[0] = "plain";
    // return "plain";
});

However I didn't get the converter right, do you know how to do it? Btw. there's no need for responseText or other string types, I just want to see the hard bytes in an ArrayBuffer! :)

I defined it using

// Install plain dataType
jQuery.ajaxSetup({
    accepts: {
        plain: "text/plain"
    },
    responseFields: {
        plain: "response"
    }
    /* , converters: {
        "* plain": function (arrayBuffer) {
            if (arrayBuffer) {
                return new Uint8Array(arrayBuffer);
            }
        }
    } /**/
});

// Handle cache's special case and global
jQuery.ajaxPrefilter("plain", function ( s, originalSettings, jqXHR ) {
    s.xhrFields = {
        responseType: "arraybuffer"
    };
    // s.dataTypes[0] = "plain";
    // return "plain";
});

However I didn't get the converter right, do you know how to do it? Btw. there's no need for responseText or other string types, I just want to see the hard bytes in an ArrayBuffer! :)

@dmethvin

This comment has been minimized.

Show comment
Hide comment
@dmethvin

dmethvin Aug 5, 2015

Member

Although #2521 didn't have quite the right approach, I think the main issue is that the normal path that the code takes generates an error when the .responseType is a blob. So it seems like we should fix only that specific problem inside jQuery, we already have a fix for IE9 in that area. Once that is done the other issues can be resolved via the approach you took in #2521, outside jQuery itself, using the extensions that $.ajax() provides.

Member

dmethvin commented Aug 5, 2015

Although #2521 didn't have quite the right approach, I think the main issue is that the normal path that the code takes generates an error when the .responseType is a blob. So it seems like we should fix only that specific problem inside jQuery, we already have a fix for IE9 in that area. Once that is done the other issues can be resolved via the approach you took in #2521, outside jQuery itself, using the extensions that $.ajax() provides.

@metadings

This comment has been minimized.

Show comment
Hide comment
@metadings

metadings Aug 13, 2015

Please, just try it. Open your console and have a look.

Please, just try it. Open your console and have a look.

@metadings metadings changed the title from XHR throws an exception accessing responseText, if responseType = arraybuffer to XHR throws an exception accessing responseText, if responseType === "arraybuffer" Sep 8, 2015

@dmethvin dmethvin referenced this issue Oct 20, 2015

Closed

Update xhr.js #2607

dmethvin added a commit to dmethvin/jquery that referenced this issue Oct 31, 2015

Ajax: Don't throw exceptions on binary data response
Fixes #2498

The added unit test shows how this could be used to support an
ArrayBuffer return, but $.ajax does not support it natively.
The goal with this change was to avoid the exception.

@dmethvin dmethvin self-assigned this Oct 31, 2015

@dmethvin dmethvin closed this in 769446c Nov 4, 2015

@dmethvin dmethvin added this to the 1.12/2.2 milestone Nov 5, 2015

@cssmagic cssmagic referenced this issue in cssmagic/ChangeLog May 18, 2016

Open

jQuery #5

@jquery jquery locked as resolved and limited conversation to collaborators Jun 19, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.