返回本机文本而不从PHP脚本解析到ajax脚本

时间:2012-10-12 03:04:55

标签: php javascript ajax innerhtml getelementbyid

您好我不知道如何通过innerhtml将完整数据返回到对象xmlhttprequest的responsetext,即没有解析。在清单1中,它有效。但是当我使用如下所示的清单2时,从php发送文本,它不起作用。清单3显示了php脚本。输出是我看到return是文本,而不是由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";';
?>

2 个答案:

答案 0 :(得分: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发送或做其他任何事情。

答案 1 :(得分: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;
?>