XMLHttpRequest 响应文本缺少字符

XMLHttpRequest responseText is missing characters

我似乎 运行 遇到了这样一个问题:调用我的 onreadystatechange 函数时只使用了我的 cgi 脚本编写的字符串的一部分。我猜 onreadystatechange 在 cgi 脚本完成其输出写入之前被调用,我想知道是否有解决办法......我有以下

      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {  
            debug("Got queryGraph response : " + xhttp.responseText);
            debug("parsing json...");
            var rtGd = JSON.parse(xhttp.responseText);
            // normal handler here...
          };
      xhttp.open("GET", "http://192.168.1.134/cgi-bin/web.cgi?queryGraph_"+timeStart+"_"+timeEnd, true);
      xhttp.send();

我得到以下调试:

Got queryGraph response : {"len":480, "msgIdx":4, "graphData":{
"Data1":[ {"t":1454247107,"v":20.19}, {"t":1454247109,"v":20.19}, 
{"t":1454247111,"v":20.19}, {"t":1454247113,"v":20.19}, 
{"t":1454247115,"v":20.19}, {"t":1454247117,"v":20.19}, 
{"t":1454247119,"v":20.19}, {"t":1454247121,"v":20.19} ], "Data2":[ 
{"t":1454247107,"v":19.94}, {"t":1454247109,"v":19.94}, 
{"t":1454247111,"v":19.94}, {"t":1454247113,"v":19.94}, 
{"t":1454247115,"v":19.94}, {"t":1454247117,"v":19.94}, 
{"t":1454247119,"v":19.94}, {"t":145424712
parsing json...

cgi脚本打印出数据,后面是XXX(调试用),但是数据结束,XXX没有出现...

    printf("\"len\":%d, \"msgIdx\":%d, ", strlen(rspData.json), msgIdx++);
    printf("\"graphData\":%s, ", rspData.json);
    printf("\"XXX\": %d, ", strlen(rspData.json));

在 onreadystatechange 回调中添加 readystate 检查。就绪状态 4 表示 "done" - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState === 4) {
    debug("Got queryGraph response : " + xhttp.responseText);
    debug("parsing json...");
    var rtGd = JSON.parse(xhttp.responseText);
    // normal handler here...
  }
};
xhttp.open("GET", "http://192.168.1.134/cgi-bin/web.cgi?queryGraph_"+timeStart+"_"+timeEnd, true);
xhttp.send();