【问题标题】:Ajax responseText and echo broken, returning header file contentsAjax responseText 和 echo 损坏,返回头文件内容
【发布时间】:2013-08-09 05:08:57
【问题描述】:

我的 thePhpFile.php 文件中有以下代码来处理 Ajax 调用:

<?php
    require_once('usefulStuff.php'); // stuff used throughout the code 

if (isset($_GET['zer']))
{

   $bFound = false;


  if(! $bFound)
  {
     echo "notfound";
     return;
  }   
  else 
  {
      echo "found";
      return;
  }
}
?>

这是处理 responseText 的内联 onreadystate 函数 (javascript):

var theResponseText = "rText";
var zer = "testing";

xmlhttp.onreadystatechange = function()
{
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        theResponseText = xmlhttp.responseText;
        alert("responseText is >>>" + theResponseText + "<<< that.");

        if( theResponseText == 'notfound')
        {
            alert("sorry, nothing found.")
        }
    }
}

var ajaxText = "thePhpFile.php?zer=" + zer;
xmlhttp.open("GET", ajaxText, false);
xmlhttp.send();

如果我真的在我的有用Stuff.php 包含文件中添加换行符或其他任何内容 - 我将它添加到有用Stuff.php 的底部, ? > 结束标记——上面的以下代码行,一个回显语句,不遗余力地定位和抓取那些额外的换行符等,并在我的 responseText 中返回它们:

 echo "notfound";

编写了一个编译器并处理了 BNF 语法,我不明白为什么 php 中的 echo 语句设置为“echo”,而不是紧跟在“echo”之后直到第一个分号 ';'在解析过程中遇到。

我知道我可以使用 trim() 来撤消空格,但我的问题是,我想强制上面的 echo 语句按照上面的 echo 语法所建议的方式进行操作 应该。如果上面的回显语句有充分的理由在我的包含文件中寻找无关的空白以返回我的“未找到”文本,我不知道那是什么原因,但我想禁用这种意外行为.

我希望我的代码行 echo "notfound"; 做到这一点,仅此而已 - 只是回显单词 notfound 并在在“未找到”文本之后立即遇到分号。

如何将 echo 行为限制为仅回显紧随单词 echo 的内容,并在到达分号时停止回显?

顺便说一句,在尝试使用 ?> 终止标记之外的有用Stuff.php 文件的内容时,我在该文件的末尾添加了以下内容:

 // now ending the php code in usefuleStuff.php:
?>
<noscript>
   <meta http-equiv="refresh" content="0; 
         URL=http://mywebsite.com/noscript.html"/>
</noscript>

代码行 echo "notfound"; -- 当我检索我的 responseText -- 响应文本还包含所有三个 noscript 代码行,以及任何额外的空白,除了我的“未找到”。

所以 php 中的“回声”是垃圾收集我在包含的有用Stuff.php 文件末尾放置的任何内容。

如何限制 echo 的行为以执行代码行 echo "notfound"; 让您相信它会做的事情,即只回显单词 "notfound" ?

【问题讨论】:

  • 常见的,只是裁剪你的问题..只是发布你的疑问的相关部分
  • 删除 echo 语句并再次运行会发生什么?
  • 我看到你们两个人说的我可能会说的,我包含的有用Stuff.php 文件的内容,该文件位于关闭 ?> 标记之外的文件末尾——即使没有回显声明,我的 responseText 仍然包含数据,我的包含文件中的关闭 ?> 标记之外的东西——谢谢,我不知道这是怎么发生的,我很感激你的解释。

标签: php javascript ajax echo


【解决方案1】:

我偶然发现了我的问题的解决方案,即 我怎样才能使声明 echo "notfound"; 在 Ajax 调用中完全按照语法似乎暗示的那样进行 -- 我很欣赏“为什么”的解释,我得到了额外的东西,简单的代码行 echo "notfound"; 乍一看并没有暗示会发生。

这就是我如何强制 echo "notfound;** 的语法做它看起来应该做的事情,即发送单词 notfound 作为我的responseText 仅此而已——我是从一篇高度外围相关的帖子中偶然发现的,该帖子只提到了 php 函数“ob_end_clean()”,然后我就从那里拿走了它。

这是我修改后的代码,它返回一个严格控制的数据块作为我的 Ajax responseText

<?php
require_once('usefulStuff.php'); // stuff used throughout the code 

if (isset($_GET['zer']))
{
   $bFound = false;


   if(! $bFound)
   {
      ob_end_clean();
      ob_start();
      echo "notfound";
      ob_end_flush();
      return;
   }   
   else 
   {
      echo "found";
      return;
   }
}
?>

为了验证这是否可行,我将十个换行符和以下代码放在了我的有用Stuff.php 文件的最后,在结束 ?> 标记之外:

   // now ending the php code in usefulStuff.php:
   ?>

    // ten newlines here....

   <noscript>
        <meta http-equiv="refresh" content="0; 
              URL=http://mywebsite.com/noscript.html"/>
   </noscript>

现在,无论我的有用Stuff.php 包含文件中的关闭 ?> php 标记之外的任何代码或空格如何——我的 Ajax onreadystatechange 函数中的 responseText 都包含我期望的内容是,“未找到”,没有别的。

自从我在 1980 年代和 1990 年代初的 C 编程时代以来,我就没有使用过输出缓冲函数。这是我第一次使用 php 的输出缓冲函数,但它确实让我能够很好地控制我的 responseText 在我的 Ajax 调用中的样子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多