Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. XMLHttpRequest problem
Qt 6.11 is out! See what's new in the release blog

XMLHttpRequest problem

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 3.5k 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.
  • G Offline
    G Offline
    GaleyevMarat
    wrote on last edited by
    #1

    Hello, I'm a newcomer in Qt Quick.
    I want to use XMLHttpRequest class in order to use Twitter API. I wrote the following code:
    @
    import QtQuick 1.0

    Rectangle {
    id: container
    width: 360
    height: 360
    Text {
    text: "Hello World"
    anchors.centerIn: parent
    }
    MouseArea {
    anchors.fill: parent
    onClicked: {
    container.testFunction();
    }
    }
    function testFunction(){
    var xhr = new XMLHttpRequest();
    xhr.onreadystatchange = function(){
    if (xmlhttp.readyState == 4) {
    if(xmlhttp.status == 200) {
    alert(xmlhttp.responseText);
    }
    }
    };
    xhr.open('GET','http://www.google.com',true);
    xhr.send();
    }
    }
    @
    But, unfortunately xmlhttp.readyState always returns "1". Would you please correct me, or drop a hint on this problem?
    Thank you!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      Shouldn't it be
      @
      if(xhr.readyState == 4)
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        Hi,

        With a few small tweaks:

        • xmlhttp -> xhr
        • alert -> console.log
        • onreadystatchange -> onreadystatechange

        The following version works for me:

        @
        import QtQuick 1.0

        Rectangle {
        id: container
        width: 360
        height: 360
        Text {
        text: "Hello World"
        anchors.centerIn: parent
        }
        MouseArea {
        anchors.fill: parent
        onClicked: {
        container.testFunction();
        }
        }
        function testFunction(){
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function(){
        console.log("readyState updated: " + xhr.readyState)
        if (xhr.readyState == 4) {
        if(xhr.status == 200) {
        console.log(xhr.responseText);
        }
        }
        };
        xhr.open('GET','http://www.google.com');
        xhr.send();
        }
        }
        @

        Does this modified version work for you, or are you still only getting a state of 1? What platform are you running on? (with e.g. Symbian it could be that your app doesn't have sufficient networking privileges?)

        Regards,
        Michael

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GaleyevMarat
          wrote on last edited by
          #4

          Thank you, Michael!!!
          I tried to run it from my office, which has a proxy configured, that's why it fails to run correctly, but then I reran it from my home laptop, with the fixes you mentioned, and it works just fine now! Thx, sir!!!

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mbrasser
            wrote on last edited by
            #5

            Great! Note that if you do need to get it working in a proxy situation, you should be able to use "QDeclarativeEngine::setNetworkAccessManagerFactory":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeengine.html#setNetworkAccessManagerFactory and the associated functionality.

            Regards,
            Michael

            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