JSON数据更新未显示在网页上

时间:2015-09-10 21:07:59

标签: javascript json ajax cors

我希望我的网页上的值发生变化,因为JSON文件中的值会发生变化。我写了一个区间函数 - intervalFunction(),但页面没有更新。这是我的代码:

<html>
   <head>
      <meta content = "text/html; charset = ISO-8859-1" http-equiv = "content-type">

      <script type = "application/javascript">
         var myVar;


         function intervalFunction() {
            myVar = setInterval(loadJSON(), 100);
         }

         function loadJSON(){
            var data_file = "http://52.21.92.17/balance";
            var http_request = new XMLHttpRequest();
            try{
               http_request = new XMLHttpRequest();
            }catch (e){
               // IE
            try{
               http_request = new ActiveXObject("Msxml2.XMLHTTP");

            }catch (e) {

               try{
                  http_request = new ActiveXObject("Microsoft.XMLHTTP");
               }catch (e){
                  alert("Your browser broke!");
                  return false;
               }

               }
            }

            http_request.onreadystatechange = function(){

               if (http_request.readyState == 4  ){
                  // Javascript function JSON.parse to parse JSON data
                  var jsonObj = JSON.parse(http_request.responseText);

                  // jsonObj variable now contains the data structure and can
                  // be accessed as jsonObj.name and jsonObj.country.
                  document.getElementById("Address").innerHTML = jsonObj[0].address;
                  document.getElementById("Balance").innerHTML = jsonObj[0].etherBalance;
               }
            }

            http_request.open("GET", data_file, true);
            http_request.send();
         }

      </script>

   </head>

   <body onload="intervalFunction()">
      <table class = "src">
         <tr><th>Address</th><th>Balance</th></tr>
         <tr><td><div id = "Address"></div></td>
         <td><div id = "Balance"></div></td></tr>
      </table>
   </body>

</html>

此外,我的另一个问题是当使用索引[1]时,我可以在网页上看到数据,但如果我使用index [0],则不会出现数组中的第一组数据。帮助将不胜感激。

0 个答案:

没有答案