I have created an application that recovers a string from a back end and puts it into a blob.
I wrote it with fetch and async-await. The problem is that EI11 doesn't support them.
I can rewrite is easily with $.ajax
GET request.
When I use fetch I can retrieve the blob from a URL with no problems.
I get an object as a response and the body as ReadableStream
.
When I use $.ajax
, I get back only a string. And when I put it in a blob, I get a different result.
For example with fetch my blob URL look like this:
Response: F)D�=C�w�Cu9mc�
Preview: F)D�=C�w�Cu9mc�
User window: F)DØ=CŠwºCu9mcœ
Content-Length: 16
Content-Type: text/html
This way I get the correct exact result as expected.
But with $.ajax
:
Response: F)D�=C�w�Cu9mc�
Preview: F)D�=C�w�Cu9mc�
User window: F)D�=C�w�Cu9mc�
Content-Length: 24
Content-Type: text/html
If I console log the blob, I also see the difference in the content length.
I would really prefer not to rewrite the application with external libraries, or adjust it to babel. But I need EI11 support.
My fetch code:
var res = null;
res = await fetch( window.location.origin +"url_path");
const blob = await res.blob();
var objectURLL = URL.createObjectURL(blob);
settings.clip.token_old = settings.blob_prefix+objectURLL.split("/")[3];
My $.ajax
code:
var blob = null;
var objectURL = null;
$.ajax({
url: "url_path"
contentType: 'text/html ; charset=utf-8',
success: function (res){
blob = new Blob([res],{type:"text/html"});
objectURL = URL.createObjectURL(blob);
},
async: false
});
Also tried:
blob = new Blob([BOM, res],{encoding:"UTF-8",type:"text/html;charset=UTF-8"});
And:
blob = new Blob([BOM, res],{type:"text/html;charset=UTF-8"});
I found a subjection to add a BOM to the blob, to force the browser to read it correctly.
Like this:
var BOM = new Uint8Array([0xEF,0xBB,0xBF]);
blob = new Blob([BOM, res],{encoding:"UTF-8",type:"text/html;charset=UTF-8"});
Which gave me this in the browser:
F)D�=C�w�Cu9mc�
Content-Length: 27
Content-Type: text/html
But it wasn't really equal the original string and does not work for me.