Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
metadings opened this issue Jul 24, 2015 · 5 comments
Assignees
Milestone

Comments

@metadings
Copy link

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
Copy link
Author

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
Copy link
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?

@metadings
Copy link
Author

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
Copy link
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
Copy link
Author

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

@metadings metadings changed the title XHR throws an exception accessing responseText, if responseType = arraybuffer XHR throws an exception accessing responseText, if responseType === "arraybuffer" Sep 8, 2015
dmethvin added a commit to dmethvin/jquery that referenced this issue Oct 31, 2015
Fixes jquery#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 added this to the 1.12/2.2 milestone Nov 5, 2015
@lock lock bot 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.
Labels
None yet
Development

No branches or pull requests

2 participants