Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › QT/QML/V-PLAY – how to enable CORS support?

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #16167

    Kacper

    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: https://jsonplaceholder.typicode.com/photos/
    I checked request by special page: https://resttesttest.com/
    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.

    f

    unction createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if (“withCredentials” in xhr) {
    // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);
    } else if (typeof XDomainRequest != “undefined”) {
    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, 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 = ‘https://jsonplaceholder.typicode.com/photos/’;

    var xhr = createCORSRequest(‘GET’, url);
    if (!xhr) {
    mylabel.text += “CORS not supported”;
    return;
    }

    // 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”;
    };

    xhr.send();
    }
    makeCorsRequest();

     

     

    #16172

    Günther
    Felgo Team

    Hi Kacper!

    QML is using Javascript as coding language, but the code is not run by a browser. The QML/Javascript code is executed by the Qt QML Interpreter, so there’s no need to differentiate between XDomainRequest and XMLHttpRequest, a simple request that works with the Qt engine is enough.

    For an example of using XmlHttpRequest you can have a look at our documentation or demo apps like the Qt World Summit demo or Twitter demo, which also access REST APIs.

    If you require to set authentication credentials, you can do so with

        httpRequest.setRequestHeader("Authorization", "Basic " + Qt.btoa("<user>:<password>")); 
    

    Does this work for you?

    Best,
    Günther from Felgo

Viewing 2 posts - 1 through 2 (of 2 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded