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: 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.

    function 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();
    

Log in to reply