Important: Please read the Qt Code of Conduct -

QT && QML how to enable CORS support?

  • I want connect to server by REST API and i need use CORS-type request.
    My CORS-type request in qml is not supported.
    For test i use page:
    I checked request by special page:
    On the page all work fine but in my application not.
    I get back "CORS not supported" (look to my code).

    What happend? Should I switch on feature in QT/QML?
    I want receive JSON form from indicated by me page.

    function createCORSRequest(method, url) {
                     var xhr = new XMLHttpRequest();
                     if ("withCredentials" in xhr) {
                       // XHR for Chrome/Firefox/Opera/Safari.
             , url, true);
                     } else if (typeof XDomainRequest != "undefined") {
                       // XDomainRequest for IE.
                       xhr = new XDomainRequest();
             , url);
                     } else {
                       // CORS not supported.
                       xhr = null;
                     return xhr;
                   // Helper method to parse the title tag from the response.
                   function getTitle(text) {
                     return text.match('<title>(.*)?</title>')[1];
                   // Make the actual CORS request.
                   function makeCorsRequest() {
                     // This is a sample server that supports CORS.
                     var url = '';
                     var xhr = createCORSRequest('GET', url);
                     if (!xhr) {
                       mylabel.text += "CORS not supported";
                     // Response handlers.
                     xhr.onload = function() {
                       var text = xhr.responseText;
                       //var title = getTitle(text);
                       mylabel.text += "Response from CORS request";
                     xhr.onerror = function() {
                         mylabel.text += "Woops, there was an error making the request";

Log in to reply