html 标签在响应标签内不起作用 AJAX PHP

html tags not working inside response tags AJAX PHP

我有一个工作代码,使用 ajax,当用户在输入框中输入内容时,它会在屏幕上输出 'hello' 而无需按回车。但是,有一个问题。让我先向您展示部分工作代码:

<?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'。我不知道为什么会这样,所以如果有人知道为什么,以及如何解决这个问题,或者如果有解决这个问题的方法,请告诉我。谢谢。

编辑:请求在名为 test5.html:

的单独文件中显示 ajax 的部分代码
<?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>]]>