【问题标题】:Return the native text WITHOUT parsing from PHP script to ajax script返回本地文本而不从 PHP 脚本解析到 ajax 脚本
【发布时间】:2012-10-02 19:55:36
【问题描述】:

您好我不知道如何通过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";';
?>

【问题讨论】:

  • 您可能希望使用
    而不是 \n,因为 html 在呈现时不会威胁换行作为显示修饰符。
  • user1739825 也许我误解了你的问题。你有没有在打印结果之前尝试输入header('Content-type: text/plain')
  • 在 javascript 中,"\n" 实际上表示换行符,因此清单 1 中的 z 应该得到一个多行字符串;但是在清单 3 中的 PHP 中,您发送的是 literal \n 和双引号。我认为这可能是您想要检查的内容。
  • 大家好刚刚发现有一篇关于这个问题的文章关于在 xmlhttprequest 发送的本机数据。尽管该解决方案不起作用,但它可能对您有用。 dygraphs.com/data.html> @Rezigned

标签: php javascript ajax innerhtml getelementbyid


【解决方案1】:

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

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 发送或执行其他任何操作。

【讨论】:

    【解决方案2】:

    我不太清楚为什么您以不同的方式执行清单 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;
    ?>
    

    【讨论】:

      猜你喜欢
      • 2014-10-18
      • 1970-01-01
      • 2011-08-08
      • 2013-07-16
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      相关资源
      最近更新 更多