Search code examples
javascriptapixmlhttprequest

MediaWiki API, polish characters


I wrote some application, which query search string for MediaWiki API and return results. However, this piece of code is not working while trying to put polish characters into URL string.

My GET request function

function GET(url) {
    var xhr = new XMLHttpRequest(); // a new request
    xhr.open("GET", url, false);
    xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8')
    try {
        xhr.send(null);
    } catch (e) {
        throw e;
    }
    return xhr.responseText;
}

I am parsing response later if that is important const resultJSON = JSON.parse(GET(searchUrl)); and this is the line my code stops working. Although the application is not crashing, but not responding. No errors. Also if the response contains polish characters it is working properly. Link for the whole context: https://hastebin.com/eqaxoxorot.js


Solution

  • Try to encode the url before sending the request, to avoid having special characters inside:

    const uri = encodeURI('http://zażółćgęsląjaźń.com');
    // result: http://za%C5%BC%C3%B3%C5%82%C4%87g%C4%99sl%C4%85ja%C5%BA%C5%84.com