html标签不工作在响应标签AJAX PHP



我有一个使用ajax的工作代码,当用户在输入框中输入一些东西时,它将在屏幕上输出'hello'而无需按enter键。然而,有一个问题。让我先展示部分工作代码:

<?php
include('head.php');
echo "<response>";
echo "hello";
echo "</response>";
?>

^——上面的代码可以工作,当用户在输入框中输入内容时,它会在屏幕上输出'hello'。输入框的代码和其他所有内容都在单独的文件中,除非请求,否则不会显示。问题是你不能在响应标签中使用html标签:

<?php
include('head.php');
echo "<response>";
echo "<i>"."hello"."</i>";
echo "</response>";
?>

^——上面的代码将输出单词"undefined",而不是斜体的单词"hello"。我不知道为什么这样做,所以如果有人知道为什么,以及如何解决这个问题,或者如果有解决这个问题的方法,请让我知道。谢谢你。

编辑:请求显示ajax的部分代码,在一个单独的文件test5.html:

<?php
include('ajax.php');
?>
<html>
<body>
<input type="text" id="userInput" onKeyUp = "process('userInput','output','foodstore.php')"/>
<div id="output" />
</body>
</html>

ajax.php文件中的更多代码:

<script type = "text/javascript">
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){ 
try{
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
    xmlHttp = false;
}
}else{ 
try{
    xmlHttp = new XMLHttpRequest();
}catch(e){
    xmlHttp = false;
}
}
if(!xmlHttp){
//alert("Cant create that object !")
}
else
return xmlHttp;
}
function process(text,output,link){
//text = 'userInput';
if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
food = encodeURIComponent(document.getElementById(text).value);
xmlHttp.open("GET", link+food,true);
xmlHttp.onreadystatechange = function() {
handleServerResponse(text,output,link);
};
xmlHttp.send(null);
}else{
setTimeout("process(text,output,link)",1000);//cekaj 1s pa probaj opet
}
}
function handleServerResponse(text,output,link){
if(xmlHttp.readyState==4){ 
if(xmlHttp.status==200){
    xmlResponse = xmlHttp.responseXML; //izvlaci se xml sto smo dobili
    xmlDocumentElement = xmlResponse.documentElement;
    message = xmlDocumentElement.firstChild.data;
    document.getElementById(output).innerHTML = message;
setTimeout(function() {
    process(text,output,link);
}, 1000);
}else{
    //alert('Someting went wrong !');
}
}
}
</script>

如何向响应标签发送数据?

你应该使用.html

你的成功函数应该是这样的:

function(data) { $('response').html(data); }

更新:XML内容必须像这样更新,以便在

中包含HTML。

<![CDATA[<i>Hello</i>]]>

最新更新