Javascript 文档末尾的额外内容-Ajax
当我运行代码时,出现以下错误: 此页面包含以下错误: 第1列第2行出错:文档末尾有额外内容 下面是第一个错误之前的页面呈现 我在两个文件中编写了代码:javascript和php,如下所示:Javascript 文档末尾的额外内容-Ajax,javascript,php,ajax,Javascript,Php,Ajax,当我运行代码时,出现以下错误: 此页面包含以下错误: 第1列第2行出错:文档末尾有额外内容 下面是第一个错误之前的页面呈现 我在两个文件中编写了代码:javascript和php,如下所示: <?php header('Content-Type: text/xml'); echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; echo '<response>';
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food = isset($_GET['food']);
$foodArray = array('tuna', 'bacon', 'beef');
if(in_array($food, $foodArray)){
echo 'We do have'.$food;
}
elseif($food==''){
echo 'Enter a food';
}else{
echo 'We dont sell'.$food;
}
echo '</response>';
?>
<html>
<head>
<script src="index.js"></script>
</head>
<body onload="process()">
<form method ="get" action="index.php"><input type="text" id="userInput"/></form>
<div id="underInput"></div>
</body>
</html>
及
var xmlHttp=createXmlHttpRequestObject();
函数createXmlHttpRequestObject(){
var-xmlHttp;
if(window.ActiveXObject){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}捕获(e){
xmlHttp=false;
}
}否则{
试一试{
xmlHttp=新的XMLHttpRequest();
}捕获(e){
xmlHttp=false;
}
}
如果(!xmlHttp){
警报(“错误”)
}否则{
返回xmlHttp;
}
}
函数过程(){
if(xmlHttp.readyState==0 | | xmlHttp.readyState==4){
food=encodeURIComponent(document.getElementById(“userInput”).value);
open(“GET”,“index.php?food=“+food,true”);
xmlHttp.onreadystatechange=handleServerResponse;
xmlHttp.send(空);
}否则{
setTimeout('process()',1000);
}
}
函数handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse=xmlHttp.responseXML;
xmlDocument=xmlResponse.documentElement;
message=xmlDocumentElement.firstChild.dataa;
document.getElementById(“underInput”).innerHTML=''+消息+'';
setTimeout('process()',1000);
}否则{
警报(“错误1”);
}
}
}
有人能帮忙吗
谢谢此错误是由于html代码引起的 制作不同的php文件。不要在其中添加html代码
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food = isset($_GET['food']);
$foodArray = array('tuna', 'bacon', 'beef');
if(in_array($food, $foodArray)){
echo 'We do have'.$food;
}
elseif($food==''){
echo 'Enter a food';
}else{
echo 'We dont sell'.$food;
}
echo '</response>';
?>
该错误是由html代码引起的 制作不同的php文件。不要在其中添加html代码
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food = isset($_GET['food']);
$foodArray = array('tuna', 'bacon', 'beef');
if(in_array($food, $foodArray)){
echo 'We do have'.$food;
}
elseif($food==''){
echo 'Enter a food';
}else{
echo 'We dont sell'.$food;
}
echo '</response>';
?>
@Mahesh我看不出我的问题的答案question@Mahesh我看不出这个问题的答案当我喝任何食物时,它应该立即返回答案当我喝任何食物时,它应该立即返回答案
<html>
<head>
<script src="xml.js"></script>
</head>
<body>
<form method ="get" action="test.php">
<input type="text" id="userInput" onkeyup = "process();"/>
</form>
<div id="underInput"></div>
</body>
</html>