Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QT && QML how to enable CORS support?
Forum Updated to NodeBB v4.3 + New Features

QT && QML how to enable CORS support?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
1 Posts 1 Posters 906 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pyroxar
    wrote on last edited by
    #1

    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();
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved