代码之家  ›  专栏  ›  技术社区  ›  GregH

为什么responseText会附加主机名和时间戳?

  •  0
  • GregH  · 技术社区  · 14 年前

    我正在尝试一个基本的Ajax/PHP测试。我有一个表单,其中有两个输入文本字段,我在其中键入两个值,一个输出文本字段,当我按下一个按钮时,输入字段连接在一起并输出到第三个文本字段。我是通过ajax/php完成的。我将连接的值输出到第三个字段,但似乎在从PHP返回的responseText上附加了一些额外的文本。附加的文本是HTML注释(<--),其中包含Web服务器的主机名和时间戳。我怎样才能摆脱这个?responseText返回responseText字符串中的附加信息是否正常?

    php/html页面如下:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>AJAX/PHP Test</title>
    
    </head>
    <body>
    
    <script language="javascript" type="text/javascript">
    // Get the HTTP Object
    function getHTTPObject(){
       if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
       else if (window.XMLHttpRequest) return new XMLHttpRequest();
       else {
          alert("Your browser does not support AJAX.");
          return null;
       }
    }
    
    // Change the value of the output field
    function setOutput() {
      var val;
      val="";
      if (httpObject.readyState == 4) {
        val=httpObject.responseText;
         if ( val != undefined ) {
           document.getElementById('outputFld').value = val;
         }
      }
    }
    
    // Implement business logic
    function doWork(){
       var url;
       httpObject = getHTTPObject();
       if (httpObject != null) {
          httpObject.onreadystatechange = setOutput;
          url="concat.php?inputText="+document.getElementById('inputFld1').value+"&inputText2="+document.getElementById('inputFld2').value;
          httpObject.open("GET", url, true);
          httpObject.send(null);
       }
    }
    
    var httpObject = null;
    
    </script>
    This is a test page to see how to get ajax and php to work together when submitting a form with data.
    
    <P>
    
    First we have a simple form.  The php will be called when the button is pressed and will concatenate
    "Input 1" and "Input 2" and write the output to the "Output" field. <P><P>
    
    
    </body>
    
    <form>
       Input 1: <input type="text" id="inputFld1"  size="50" /><br>
       Input 2: <input type="text" id="inputFld2"  size="50" /><br>
    <HR>
       Output: <input type="text" id="outputFld"  size="100" /><br>
    <P>
       <input type="button" name="submitButton" value="Concatenate" onClick="doWork()" />
    
    
    </html>
    

    在URL中使用open调用(concat.php)调用的php如下:

    <?php
      $in1 = $_GET['inputText'];
      $in2 = $_GET['inputText2'];
      $returnvar = $in1 . ' - ' . $in2;
      echo $returnvar;
    ?>
    

    在responseText中返回的内容(如果我的两个输入字段包含“one”和“two”)是:

    ONE - TWO<!-- webserver1.thedomain.com compressed/chunked Thu Jul  1 15:42:08 PDT 2010 -->
    

    在responseText上附加的“”注释是什么?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Killer_X    14 年前

    在某些配置(php、apache…)中,“自动附加文件”范围内的设置是否处于活动状态?或者您正在使用的Web服务器或某个扩展名导致字符串被附加?

    我快速地谷歌了一下,但找不到任何真正的线索。我得到的大多数参考资料都指向人们从不同的雅虎服务得到的一些回复。

        2
  •  0
  •   Jake N    14 年前

    我刚刚运行了这个,它按我的预期工作,没有日期和域。

    为什么不尝试使用jquery、moo工具或原型之类的Ajax支持?