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. Why I can't assign the "th" variant in this JS function?
QtWS25 Last Chance

Why I can't assign the "th" variant in this JS function?

Scheduled Pinned Locked Moved General and Desktop
qmljson parserassign
6 Posts 3 Posters 2.0k 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.
  • D Offline
    D Offline
    DidaHarp
    wrote on last edited by DidaHarp
    #1

    I want to get JSON data from the Internet,and I write a js function to load the JSON data to my ListModel.But there is an unexpected error, which is ,var "th" can't be assigned in onreadystatechange function.What happened?
    Here is the function:
    function getPictureURLByID(idstr){
    var beforeID = "http://ting.baidu.com/data/music/links?songIds=";
    var sea = beforeID + idstr; // "idstr" is a id string that I get from another JS function,such like:2152968,107184439,91590745,...
    var th = "";

       var here = new XMLHttpRequest();
       here.open("GET",sea);
       here.onreadystatechange=function(){
           if (here.readyState == XMLHttpRequest.DONE){
               var there = JSON.parse(here.responseText);   
               th = there.data.songList[0].songPicSmall; //"th" is assigned to the URL of certain image
               console.log(th);  // this prints something like :http://a.hiphotos.baidu.com/ting/pic/item/cdbf6c81800a19d80b7d421131fa828ba61e46ac.jpg
           }
        }
       here.send();
       console.log(th);  //while this prints nothing,just an empty string
       return th;  
    

    }

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DidaHarp
      wrote on last edited by
      #2

      Can anyone help?

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

        You should exercise some patience. Typical rule in the past was to wait at least a day or a couple of them.

        Unfortunately, I cannot help you with this. Possibly a stupid question, can't you use a debugger?
        In C++ I would simply set a break point around and check if the program code is executed as expected.

        Vote the answer(s) that helped you to solve your issue(s)

        D 1 Reply Last reply
        0
        • L Offline
          L Offline
          Leonardo
          wrote on last edited by
          #4

          I agree with @koahnig . Be patient.

          Regarding your question, XMLHttpRequest performs an asynchronous request. "th" is empty right after send() because onreadystatechange wasn't triggered yet. You'd better have a callback function.

          function getTh(onThReady){
             var here = new XMLHttpRequest();
             here.open("GET",sea);
             here.onreadystatechange=function(){
                 if (here.readyState == XMLHttpRequest.DONE){
                     var there = JSON.parse(here.responseText);   
                     onThReady(there.data.songList[0].songPicSmall);
                 }
              }
             here.send();
          }
          ...
          getTh(function(th){ console.log(th); });
          
          D 1 Reply Last reply
          0
          • K koahnig

            You should exercise some patience. Typical rule in the past was to wait at least a day or a couple of them.

            Unfortunately, I cannot help you with this. Possibly a stupid question, can't you use a debugger?
            In C++ I would simply set a break point around and check if the program code is executed as expected.

            D Offline
            D Offline
            DidaHarp
            wrote on last edited by
            #5

            @koahnig Well,I didn't think of using a debugger. I just can't imagine such an assignment problem have to use a debugger. Right now ,I'm going to use that.

            1 Reply Last reply
            0
            • L Leonardo

              I agree with @koahnig . Be patient.

              Regarding your question, XMLHttpRequest performs an asynchronous request. "th" is empty right after send() because onreadystatechange wasn't triggered yet. You'd better have a callback function.

              function getTh(onThReady){
                 var here = new XMLHttpRequest();
                 here.open("GET",sea);
                 here.onreadystatechange=function(){
                     if (here.readyState == XMLHttpRequest.DONE){
                         var there = JSON.parse(here.responseText);   
                         onThReady(there.data.songList[0].songPicSmall);
                     }
                  }
                 here.send();
              }
              ...
              getTh(function(th){ console.log(th); });
              
              D Offline
              D Offline
              DidaHarp
              wrote on last edited by
              #6

              @Leonardo such a small but knotty thing...

              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