Skip to main content

Bug Tracker

Side navigation

#11461 closed enhancement (cantfix)

Opened March 09, 2012 03:04PM UTC

Closed March 12, 2012 04:10PM UTC

Last modified February 22, 2014 03:56PM UTC

Add support for HTML5 XHR v2 with responseType set to 'arraybuffer' on $.ajax

Reported by: alessandro.aglietti@gmail.com Owned by:
Priority: low Milestone: None
Component: ajax Version: 1.7.1
Keywords: Cc: jaubourg
Blocked by: Blocking:
Description

I try to use $.ajax for retrieving binary data as explain in HTML5 http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#the-responsetype-attribute.

And I try with these options:

$.ajax({
		url : "/blob/job.pdf",
		success : onSuccess,
		type : 'GET',
		xhrFields : {
			responseType : 'arraybuffer'
		},
		error : onError
});

jQuery raise an error because try to access ''xhr.responseXML'' or ''xhr.responseText'' but this properties are not accessible, instead we need to use ''xhr.response''.

I resolved with this code from line 8143 to 8154

if ( s.dataType == 'binary' ) {
	//USE XHR v2 with binary data
	responses.binary = xhr.response;
} else {
	xml = xhr.responseXML;

	// Construct response list
	if ( xml && xml.documentElement /* #4958 */ ) {
		responses.xml = xml;
	}
	responses.text = xhr.responseText;
}

Now when I will to use XHR with binary response add a dataType 'binary' to the $.ajax():

$.ajax({
		url : "/blob/job.pdf",
		success : onSuccess,
		type : 'GET',
		xhrFields : {
			responseType : 'arraybuffer'
		},
		error : onError,
		dataType : 'binary'
	});

I hope this is useful for the community.

See you soon,

Attachments (0)
Change History (11)

Changed March 09, 2012 03:17PM UTC by addyosmani comment:1

cc: → jaubourg
component: unfiledajax
priority: undecidedlow

Changed March 12, 2012 04:10PM UTC by rwaldron comment:2

resolution: → cantfix
status: newclosed

There is no way to create a backwards compatible implementation

Changed March 15, 2012 09:21AM UTC by alessandro.aglietti@gmail.com comment:3

Replying to [comment:2 rwaldron]:

There is no way to create a backwards compatible implementation

Excuse me, but my fix is just backward compatible. Because the old request continuos to work without problem.

Hugs

Changed March 22, 2012 02:43PM UTC by jaubourg comment:4

Your fix doesn't take dataType chaining in consideration (like "type1 type2").

Handle the binary dataType should be possible but god, does the XHR spec make things cumbersome. You may wanna open an issue with ajaxHooks ( https://github.com/jaubourg/ajaxHooks ). We could try and find the best possible solution there.

Replying to [comment:3 alessandro.aglietti@…]:

Replying to [comment:2 rwaldron]: > There is no way to create a backwards compatible implementation Excuse me, but my fix is just backward compatible. Because the old request continuos to work without problem. Hugs

Changed April 14, 2012 07:34AM UTC by spongman comment:5

is there any timeline for adding binary support to jQuery?

for now, i'm using alessandro's patch (thanks). i don't care about backward compatibility - i don't even know what 'dataType chaining' is, or why i should care.

Changed April 18, 2012 02:09PM UTC by robertedgar@netvigator.com comment:6

Count me as another one currently using allessandro's patch or a variation of but wanting an 'official' fix ASAP.

I will say I don't see why the proposed solution would not be backwards compatible.

At the end of they day the only time this is going to be used by anyone is when they will know exactly what type of response they are going to get before the call starts, so setting any flags needed to tell jQ to not process the return is easy. If they are not set you just do as you did before.

I really can't see that this is that hard to resolve it has got to be one of the more trivial things that get requested.

That said I think it would be better to directly check xhr.responseType rather than s.dataType as I want dataType to indicate the particular subtype of binary data being returned which I may want to process in a converter.

Rob

Changed April 18, 2012 03:16PM UTC by jaubourg comment:7

It's exactly because of the conversion logic that we cannot apply allessandro's fix. There is more here than what catch the eye (mainly because of the different binary formats). The patch is good in a 1.4.x ad-hoc/unflexible kind of way.

I'd love to immerse myself into this binary data business but I'm very very busy at work right now. One thing is for certain, the xhr spec continues to be one of the most convoluted spec I've ever encountered and it just bleeds through any abstraction on top.

Replying to [comment:6 robertedgar@…]:

Count me as another one currently using allessandro's patch or a variation of but wanting an 'official' fix ASAP. I will say I don't see why the proposed solution would not be backwards compatible. At the end of they day the only time this is going to be used by anyone is when they will know exactly what type of response they are going to get before the call starts, so setting any flags needed to tell jQ to not process the return is easy. If they are not set you just do as you did before. I really can't see that this is that hard to resolve it has got to be one of the more trivial things that get requested. That said I think it would be better to directly check xhr.responseType rather than s.dataType as I want dataType to indicate the particular subtype of binary data being returned which I may want to process in a converter. Rob

Changed February 13, 2013 10:27PM UTC by anonymous comment:8

Hi, just a note (Feb 2013) still using this approach, works perfectly for what I need it for, the modifications for jQuery 1.9.1 are:

line ~8564

      if ( s.dataType == 'binary' ) {
            // use XHR2 with binary data
            responses.binary = xhr.response;
      } else {
      
            // When requesting binary data, IE6-9 will throw an exception
            // on any attempt to access responseText (#11426)
            if ( typeof xhr.responseText === "string" ) {
                  responses.text = xhr.responseText;
            }
      }

and

      if ( !status && s.isLocal && !s.crossDomain ) {
            if ( s.dataType == 'binary' ) {
                  status = responses.binary ? 200 : 404;
            } else {
                  status = responses.text ? 200 : 404;
            }
          ...

Changed May 15, 2013 11:41PM UTC by anonymoustoo comment:9

Thanks for posting the mod for 1.9.1 - great pity such an important request has been left as closed.

Changed February 01, 2014 03:13AM UTC by domanite comment:10

_comment0: I just hit this too, and am also bummed I can do binary data with jquery.1391224519678155

I just hit this too, and am also bummed I cannot do binary data with jquery.

Changed February 22, 2014 03:56PM UTC by tarik.benmerar comment:11

Hello,

Please, take a look at this pull request :

https://github.com/jquery/jquery/pull/1525

I invite everyone to join the discussion.