Force encoding used in XHR request

4.5k views Asked by At

In a browser extension I'm developing, I'm doing an XHR request to load some data, using jQuery:

$.get(url).done(function(data, textStatus) {
    console.log(data);
})

The remotely-loaded data is a windows-1252 encoded CSV file served with Content-disposition:attachment and without mime-type/charset header (I don't have control on the backend, so I can't fix that).

How can I force the browser to decode the response as windows-1252 instead of utf-8 as it apparently currently does?

4

There are 4 answers

1
Laurent VB On BEST ANSWER

As hinted by my previous research and the first answers, I couldn't find a way to do what I wanted using jQuery. I worked around the issue by using a vanilla XMLHttpRequest with responseType=blob, as explained in https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data

var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "blob";
oReq.onload = function(e) {
    var blob = new Blob([oReq.response], { type : 'text\/csv' });
    account.data = blob;
}
oReq.onerror = function(e){
    ...
}
oReq.send();

Fortunately, I was already using a Blob to post the data back to the server so I'm actually saving a decoding/encoding step here...

0
Yeronimo On

If you set the correct encoding on the request doesn't jQuery use the same encoding for the response?

So something along the lines of:

$.ajax({
    type: "GET",
    url: "test.php",
    contentType: "application/x-www-form-urlencoded;charset=windows-1252",
    dataType: 'json',
    success: function(data) {
            console.log(data);
    }
});
1
patrick On

I'm not sure if it is possible. The doc says that you can specify the charset, but the browser will always make the request as UTF-8 (per spec):

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

Type: String When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.

So the code will be like this:

$.ajax({
    type: 'GET',
    url: url,
    contentType: "application/x-www-form-urlencoded; charset=windows-1252",
    dataType: 'json',
    success: function(data) {
        console.log(data);
    }
});

if this doesn't work you should create a "proxy" which converts your file from windows-1252 to utf-8

0
Michael On

Maybe the followings also can be useful for somebody.

function getData(url) {
  var request = new XMLHttpRequest();
  request.open('GET', url, false);
  request.overrideMimeType('text/xml; charset=iso-8859-1');
  request.send(null);
  if (request.status === 200) {
    console.log(request.responseText);
  }
}