Search code examples
javascriptcharacter-encodingxmlhttprequestwin-universal-appwinjs

WinJS.xhr not reading foreign characters


I'm making a WinJS UWP app and I got myself a xhr that returns a source of a HTML page

WinJS.xhr({
        type: "get",
        url: "http://rozvrhuni.hys.cz/150909_2.html", 
        headers: "Accept-Charset= windows-1250, Accept-Language= cs",
    }).done(function (result) {
        htmlText = result.responseText.toString();
    },
    function error(request) {
        (new Windows.UI.Popups.MessageDialog("Non-existant content", "Error")).showAsync().done();
        return;
    });

Example of the values gotten: "Bakal��i - Suplov�n�"

The original text: "Bakaláři - Suplování"

The page is Content-type: text/html; charset= windows-1250. Content-language: cs.

As you can see I tried using the headers to read it correctly but nothing of what I tried worked and sometimes the request failed whatsoever after applying them.

I'm fairly new to http requests and related stuff so most of what I've done is put together from examples or solutions online.

What do I need to do for the request to read the page correctly?


Solution

  • After forever of looking everywhere, I tried this and it worked...

    var xhr = new XMLHttpRequest();
        xhr.open("GET", "http://rozvrhuni.hys.cz/150909_2.html", false, document);
        xhr.setRequestHeader("Content-Type", "text/html; charset=windows-1250");
        if (xhr.overrideMimeType) xhr.overrideMimeType("text/html; charset=windows-1250");
        try {
            xhr.send()
            htmlText = xhr.responseText;
            //Work with htmlText
        } catch (e) {
            (new Windows.UI.Popups.MessageDialog("Something went wrong", "Error")).showAsync().done();
            return;
        }