dongya8378 2015-12-11 17:04
浏览 102
已采纳

JavaScript XMLHttpRequest覆盖以前的responseText

I need to do two squential XMLHttpRequests via JavaScript, for data update on two different JavaScript charts, displayed on this php page:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="canvasjs.min.js"></script>
<script type="text/javascript" src="AJAXrestFunc.js"></script>
<script type="text/javascript">
        window.onload = function () {
            var updateInterval = 1000;  // milliseconds
            var xhttp1, xhttp2
            function getXHR() {
                if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                    return new XMLHttpRequest();
                }
                else { // code for IE6, IE5
                    return new ActiveXObject("Microsoft.XMLHTTP");
                }
            }
            xhttp1 = getXHR();
            setInterval(function(){updateChart("started", xhttp1)}, updateInterval);
            xhttp2 = getXHR();
            setInterval(function(){updateChart("finished", xhttp2)}, updateInterval);
    }
</script>
</head>
<body>
    <div id="started" style="height:300px; width:100%;"></div>
    <div id="finished" style="height:300px; width:100%;"></div>
</body>
</html>

I tried to create two different XMLHttpRequest with the function getXHR. The paramater "started"\"finished" in the function updateChart, defines the REST call that I need for the update task.

Here is the updateChart:

function updateChart (func, xhttp) {
     jsonresponse = restCall(func, xhttp);
     console.log(jsonresponse);
     console.log(func);
     parsed = JSON.parse(jsonresponse);
     dps = [];
     count = 0;
     var sum = 0;
     for (var key in parsed) {
         dps[count] = {label: "TaskExecutor"+key, y: parsed[key]};
         count++;
         sum = sum + parsed[key];
     }

     var totalTask = "Total Tasks "+func+" "+sum;

     var chart = new CanvasJS.Chart(func,{
          theme: "theme4",
          title:{text: "Task "+func},
          axisY: {title: func},
          legend:{
              verticalAlign: "top",
              horizontalAlign: "centre",
              fontSize: 18
          },
          data : [{
              type: "column",
              showInLegend: true,
              legendMarkerType: "none",
              legendText: totalTask,
              indexLabel: "{y}",
              dataPoints: dps
          }]
      });

     chart.render();

  };

which calls the function restCall(), here defined:

 function restCall(func, xhttp) {
     xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
             content = xhttp.responseText; //here I get the responseText
         }
     };
     xhttp.open("GET", "/testingREST/monitor/all_"+func, true);
     xhttp.send();
     return content;
     }

I don't know why, but the chart gets and display the last responseText from the xhttp object (like It's overwritten) of the two restcall (even if I reverse the order request, the last call wins).

Infact, when I log the response (in the first three rows updateChart()), I got something like:

"{"1":2,"2":3,"3":10,"5":7,"12":4}" AJAXrestFunc.js:14:4
"started" AJAXrestFunc.js:15:4
"{"1":2,"2":3,"3":10,"5":7,"12":4}" AJAXrestFunc.js:14:4
"finished" AJAXrestFunc.js:15:4
"{"1":2,"2":3,"3":10,"5":7,"12":4}" AJAXrestFunc.js:14:4
"started" AJAXrestFunc.js:15:4
"{"1":2,"2":3,"3":10,"5":7,"12":4}" AJAXrestFunc.js:14:4
"finished" AJAXrestFunc.js:15:4
"{"1":2,"2":3,"3":10,"5":7,"12":4}" AJAXrestFunc.js:14:4
"started" AJAXrestFunc.js:15:4
"{"1":2,"2":3,"3":10,"5":7,"12":4}" AJAXrestFunc.js:14:4
"finished"
...

"{"1":2,"2":3,"3":10,"5":7,"12":4}" is the JSON response which I got when I call the rest function "finished". But there isn't the JSON "started" response. Maybe it's overwritten? Someone knows why?

  • 写回答

1条回答 默认 最新

  • douzi8112 2015-12-14 12:46
    关注

    As other users pointed out, the problem was the setting of a global variable (content) which was overwritten at each call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行