我正在尝试一个基本的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上附加的“”注释是什么?