Search code examples
internet-explorercharacter-encodingxmlhttprequestwindows-1252

Override incorrect declared text encoding in XML document using XMLHttpRequest


Our app is receiving data from a source with incorrect XML headers. Although the workaround in that post works (inserting an nginx proxy), we'd like to find a client-side solution, if there is one.

So, is there a way to intercept an XML document and force the document to be interpreted as Windows 1251 rather than UTF-8? XMLHttpRequest has an "overrideMimeType" property, but not an "overrideEncoding".

All of this matters primarily for IE, which completely fails to parse the document if it hits a single impossible character. Chrome and Firefox just show question mark characters, which is fine.


Solution

  • if you initially read the file with xhr.responseType="arraybuffer", then blob it with your desired mime/charset like such:

    new Blob([ arraybuff ], {type: 'application/xml;charset=windows-1251'}) 
    

    then create a blobURL for it via URL.createObjectURL(blob), and finally, xhr that, I think it will work.

    Here's an online test.