2

您好我不知道如何通过 innerhtml 将完整的数据返回给对象 xmlhttprequest 的 responsetext,即无需解析。在清单 1 中,它可以工作。但是当我使用如下所示的清单 2 从 php 发送文本时,它不起作用。清单 3 显示了 php 脚本。输出是我看到返回是文本,而不是由 dygraph 函数处理提前谢谢。

Listing1:-
    xmlhttp.onreadystatechange=function()
     {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
             z = new Dygraph(document.getElementById("showrealchart"),
                  "Batch,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,\n" +
"1, 3.65, 5.00, 4.53, 5.01, 10.50, 0.03, 9.05, 5.05, 5.22, 6.23\n"+
"7, 3.65, 5.03, 4.50, 5.02, 9.50, 0.05, 9.15, 5.55, 5.20, 6.23\n"+
"8, 3.67, 5.00, 4.53, 4.99, 9.00, 0.04, 9.30, 5.10, 2.30, 6.22\n"+
"12, 3.65, 5.04, 4.53, 4.99, 10.05, 0.35, 9.00, 5.23, 5.20, 6.21\n"+
"16, 3.66, 5.00, 4.50, 4.98, 10.50, 1.01, 9.01, 5.20, 5.10, 6.24\n"+
"18, 3.65, 5.02, 4.70, 5.00, 9.80, 0.45, 9.14, 5.63, 5.15, 6.23\n");
      }
     }

现在我想以确切的形式返回数据。

Listing 2:-
xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                z = new Dygraph
                document.getElementById("showrealchart").innerHTML=xmlhttp.responseText;

和 PHP 脚本。

Listing 3;-
<?php
print '"Batch,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,\n" +
"1, 3.65, 5.00, 4.53, 5.01, 10.50, 0.03, 9.05, 5.05, 5.22, 6.23\n"+
"7, 3.65, 5.03, 4.50, 5.02, 9.50, 0.05, 9.15, 5.55, 5.20, 6.23\n"+
"8, 3.67, 5.00, 4.53, 4.99, 9.00, 0.04, 9.30, 5.10, 2.30, 6.22\n"+
"12, 3.65, 5.04, 4.53, 4.99, 10.05, 0.35, 9.00, 5.23, 5.20, 6.21\n"+
"16, 3.66, 5.00, 4.50, 4.98, 10.50, 1.01, 9.01, 5.20, 5.10, 6.24\n"+
"18, 3.65, 5.02, 4.70, 5.00, 9.80, 0.45, 9.14, 5.63, 5.15, 6.23\n";';
?>
4

2 回答 2

0

您正在返回一个字符串文字。这不起作用,因为您实际上会得到:

document.getElementById("showrealchart").innerHTML="\"Batch, ....\"";

请注意引号是双引号。

如果您从 PHP 中删除引号,如下所示:

print 'Batch,S1,S2...\n1 3.65...\n';

您的 javascript 回调会将其作为要使用的字符串。

或者,如果您想要 PHP 中的长版本,您可以执行以下操作之一:

$res = print 'Batch,S1,S2...\n' .
       '1, 3.65, ...\n' .
       ....;
print $res;

但也取决于您的客户端 Js 对数据的处理方式,您可能想要使用 \n 位,将所有这些作为 JSON 发送或执行其他任何操作。

于 2012-10-12T07:10:11.420 回答
0

我不太清楚为什么您以不同的方式执行清单 1 和 2 中的代码。但我相信这就是你想要的。

清单 2。

var z = new Dygraph(document.getElementById("showrealchart"), xmlhttp.responseText);

清单 3。

<?php
# ... is the rest of your data
echo <<<TXT
Batch,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10
1, 3.65, 5.00, 4.53, 5.01, 10.50, 0.03, 9.05, 5.05, 5.22, 6.23
7, 3.65, 5.03, 4.50, 5.02, 9.50, 0.05, 9.15, 5.55, 5.20, 6.23
...
...
TXT;
?>
于 2012-10-12T10:14:23.437 回答