Bug Tracker

Modify

Ticket #11461 (closed enhancement: cantfix)

Opened 15 months ago

Last modified 6 days ago

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

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

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,

Change History

comment:1 Changed 15 months ago by addyosmani

  • Cc jaubourg added
  • Priority changed from undecided to low
  • Component changed from unfiled to ajax

comment:2 follow-up: ↓ 3 Changed 15 months ago by rwaldron

  • Status changed from new to closed
  • Resolution set to cantfix

There is no way to create a backwards compatible implementation

comment:3 in reply to: ↑ 2 ; follow-up: ↓ 4 Changed 14 months ago by alessandro.aglietti@…

Replying to 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

comment:4 in reply to: ↑ 3 Changed 14 months ago by jaubourg

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 alessandro.aglietti@…:

Replying to 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

comment:5 Changed 13 months ago by spongman

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.

comment:6 follow-up: ↓ 7 Changed 13 months ago by 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

comment:7 in reply to: ↑ 6 Changed 13 months ago by jaubourg

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 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

comment:8 Changed 3 months ago by anonymous

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;
            }
          ...

comment:9 Changed 6 days ago by anonymoustoo

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.