Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to enable CORS support in application in QT?
Forum Updated to NodeBB v4.3 + New Features

How to enable CORS support in application in QT?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.8k Views 1 Watching
  • 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 add CORS support for my application (QML and xhtmlrequest, JSON, RESTAPI, etc)
    I finded this: http://doc.qt.io/qt-5/qwebsocketcorsauthenticator.html
    but i dont have idea how i should do it.

    in main.cpp:

    QWebSocketCorsAuthenticator CORS;
    CORS.setAllowed(true);
    

    Can you give me a clue?

    mrjjM 1 Reply Last reply
    0
    • P Offline
      P Offline
      Pyroxar
      wrote on last edited by
      #5

      I use QML/XMLHttpRequest

      var req = new XMLHttpRequest();
                     req.open('GET', 'http://www.kacperpaczos.pl/skryptwphpie/?cors', true); /* Argument trzeci, wartość true, określa, że żądanie ma być asynchroniczne */
                     req.onreadystatechange = function(aEvt) {
                         if (req.readyState == 4) {
                             if (req.status == 200) {
                                 //showRequestInfo(req.responseText);
                                 mylabel.text = req.responseText;
                             } else{
                                 //showRequestInfo("Błąd podczas ładowania strony\n");
                             mylabel.text = "Błąd podczas ładowania strony\n";}
                         }
                     };
      

      It work fine

      1 Reply Last reply
      0
      • P Pyroxar

        I want add CORS support for my application (QML and xhtmlrequest, JSON, RESTAPI, etc)
        I finded this: http://doc.qt.io/qt-5/qwebsocketcorsauthenticator.html
        but i dont have idea how i should do it.

        in main.cpp:

        QWebSocketCorsAuthenticator CORS;
        CORS.setAllowed(true);
        

        Can you give me a clue?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Pyroxar
        Hi
        It seems its via a signal.

        "the QWebSocketCorsAuthenticator class is used in the originAuthenticationRequired() signal"

        http://doc.qt.io/qt-5/qwebsocketserver.html#originAuthenticationRequired

        So you connect the signal to a slot and when its called, you use the
        authenticator parameter and call setAllowed() on that one.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #3

          I found this in "QML Global Object" documentation:

          "QML's XMLHttpRequest does not enforce the same origin policy."

          1 Reply Last reply
          1
          • P Offline
            P Offline
            Pyroxar
            wrote on last edited by Pyroxar
            #4

            I read something:
            http://doc.qt.io/qt-5/echoserver.html
            http://doc.qt.io/qt-5/signalsandslots.html

            In 'main.cpp' file I 'copmpose' this:

            QWebSocketCorsAuthenticator *CORS = new QWebSocketCorsAuthenticator("https://jsonplaceholder.typicode.com/photos/");
                CORS->setAllowed(true);
                 //qDebug() << CORS.allowed();
            
                QWebSocketServer *CORSrequest = new QWebSocketServer(QStringLiteral("Request"),
                                                                     QWebSocketServer::SecureMode);
                CORSrequest->originAuthenticationRequired(CORS);
            
                QObject::connect(CORSrequest, &QWebSocketServer::originAuthenticationRequired,
                                     CORS, &QWebSocketCorsAuthenticator::allowed);
            

            Is this the right way?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Pyroxar
              wrote on last edited by
              #5

              I use QML/XMLHttpRequest

              var req = new XMLHttpRequest();
                             req.open('GET', 'http://www.kacperpaczos.pl/skryptwphpie/?cors', true); /* Argument trzeci, wartość true, określa, że żądanie ma być asynchroniczne */
                             req.onreadystatechange = function(aEvt) {
                                 if (req.readyState == 4) {
                                     if (req.status == 200) {
                                         //showRequestInfo(req.responseText);
                                         mylabel.text = req.responseText;
                                     } else{
                                         //showRequestInfo("Błąd podczas ładowania strony\n");
                                     mylabel.text = "Błąd podczas ładowania strony\n";}
                                 }
                             };
              

              It work fine

              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