【问题标题】:Why does responseText have the hostname and timestamp appended to it?为什么 responseText 附加了主机名和时间戳?
【发布时间】:2010-07-02 07:51:40
【问题描述】:

我正在尝试一个基本的 AJAX/php 测试。我有一个带有两个输入文本字段的表单,我在其中输入两个值和一个输出文本字段,当我按下按钮时,输入字段连接在一起并输出到第三个文本字段。我正在通过 AJAX/PHP 执行此操作。我将连接值输出到第三个字段,但似乎有一些附加文本附加到我从 PHP 返回的 responseText 上。附加的文本是带有 Web 服务器主机名和时间戳的 HTML 注释 (

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<!-- webserver1.thedomain.com compressed/chunked Thu Jul  1 15:42:08 PDT 2010 -->

responseText 后面的“”注释是什么意思?

【问题讨论】:

    标签: php ajax


    【解决方案1】:

    是否有可能在某些配置(PHP、apache...)中“自动附加文件”范围内的设置处于活动状态?或者您正在使用的网络服务器或某些扩展程序导致字符串被附加?

    我做了一些快速的谷歌搜索,但找不到对字符串的任何真实引用。我得到的参考资料最多的是人们从不同的雅虎服务中得到的一些回复。

    【讨论】:

    • 我认为你可能是对的。我们这里可能对 PHP 或 apache 有特殊的设置。
    • 关于您的问题“我如何摆脱它?”:如果您只是将内容作为 html 添加到页面中,则无论如何都会对其进行评论,否则一些 JS“评论删除”正则表达式魔法可能求助:w3schools.com/jsref/jsref_obj_regexp.asp
    【解决方案2】:

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

    为什么不尝试使用 jQuery、Moo Tools 或 Prototype 之类的 AJAX 支持?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 2019-11-08
      相关资源
      最近更新 更多