#11461 closed enhancement (cantfix)
Add support for HTML5 XHR v2 with responseType set to 'arraybuffer' on $.ajax
Reported by: | 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,
Change History (11)
comment:1 Changed 11 years ago by
Cc: | jaubourg added |
---|---|
Component: | unfiled → ajax |
Priority: | undecided → low |
comment:2 follow-up: 3 Changed 11 years ago by
Resolution: | → cantfix |
---|---|
Status: | new → closed |
comment:3 follow-up: 4 Changed 11 years ago by
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 Changed 11 years ago by
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 11 years ago by
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 11 years ago by
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 Changed 11 years ago by
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 10 years ago by
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 10 years ago by
Thanks for posting the mod for 1.9.1 - great pity such an important request has been left as closed.
comment:10 Changed 9 years ago by
I just hit this too, and am also bummed I cannot do binary data with jquery.
comment:11 Changed 9 years ago by
Hello, Please, take a look at this pull request : https://github.com/jquery/jquery/pull/1525
I invite everyone to join the discussion.
There is no way to create a backwards compatible implementation