Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ajax responseText和echo已损坏,返回头文件内容_Php_Javascript_Ajax_Echo - Fatal编程技术网

Php Ajax responseText和echo已损坏,返回头文件内容

Php Ajax responseText和echo已损坏,返回头文件内容,php,javascript,ajax,echo,Php,Javascript,Ajax,Echo,我在TheppFile.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

我在TheppFile.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;
  }
}
?>
在编写了一个编译器并处理了BNF语法之后,我不明白为什么php中的echo语句被设置为“echo”,而不仅仅是在“echo”后面加上第一个分号“;”在解析过程中遇到的

我知道我可以使用trim()撤消空白,但我的问题是,我想强制上面的echo语句按照上面的echo语法建议的方式进行操作。如果上面的echo语句在我的include文件中寻找多余的空白以与我的“notfound”文本一起返回,我不知道这是什么原因,但我想禁用这种意外行为

我想要我的代码行回显“notfound”仅此而已——只需回显单词notfound,并在“notfound”文本后立即遇到分号时停止回显

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

顺便说一句,在试验usefulStuff.php文件中不属于?>终止标记的内容时,我在该文件末尾添加了以下内容:

 // now ending the php code in usefuleStuff.php:
?>
<noscript>
   <meta http-equiv="refresh" content="0; 
         URL=http://mywebsite.com/noscript.html"/>
</noscript>
//现在在usefuleStuff.php中结束php代码:
?>
代码行回显“notfound”--当我检索我的responseText时--响应文本还包含所有三行noscript代码,以及除我的“notfound”之外的任何额外空格

因此,php中的“echo”是垃圾收集我在包含的usefulStuff.php文件末尾放入的任何内容


我如何限制echo的行为来执行代码行echo“notfound”的操作使您相信它会起作用,也就是说,只回显“未找到”一词

我无意中发现了一个问题的解决方案,那就是我如何才能做出回应“notfound”的陈述在Ajax调用中,完全按照语法的建议去做——我很欣赏对“为什么”的解释,我得到了一些简单的代码行回显“notfound”的额外内容乍一看并不表明会发生这种情况

下面是我如何强制echo“notfound*”的语法来做它看起来应该做的事情,也就是将单词notfound作为我的responseText发送,而不是别的——我在一篇与外围设备高度相关的文章中偶然发现了这一点,其中只提到了php函数“ob_end_clean()“我从那里拿走了

下面是我修改过的代码,它返回一个严格控制的数据块作为我的AjaxresponseText

<?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;
   }
}
?>

为了验证这一点,我在usefulStuff.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>
//现在在usefulStuff.php中结束php代码:
?>
//这里有十条新线。。。。
现在,不管在我的usefulStuff.php include文件中我的closing?>php标记之外有什么代码或空格,我的Ajax onreadystatechange函数中的responseText完全包含我期望的内容,“notfound”,没有其他内容


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

常见,只需裁剪你的问题。。只需发布你的疑问的相关部分。什么当你删除echo语句并再次运行它时会发生什么?我完全明白你们两个家伙所说的,我的include'd usefulStuff.php文件的内容,它位于文件末尾的closing?>标记之外--即使没有echo语句,我的responseText仍然包含数据,closing之外的东西?>标记在我的include文件中--谢谢嗯,我不知道这是怎么发生的,我很感谢你的解释。
<?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;
   }
}
?>
   // 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>