这是HTML文件(namestore.html): -
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ajax Practice</title>
<script type="text/javascript" src="namestore.js"></script>
</head>
<body>
<div onkeyup="process()">
Enter your name:<input type="text" id="userInput"/>
<br /><br />
<div id="outputMsg" />
</div>
</body>
</html>
这是PHP文件(namestore.php): -
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<xtag>';
$x = 1;
echo '</xtag>';
echo '<response>';
$name = $_GET['name'];
$nameArray = array('Raj','John','Sam','Mac');
if(in_array($name,$nameArray))
{
echo 'Wasup'.' '. $name;
}
elseif($name=='')
{
echo 'Enter a name pal!';
}
else
{
echo 'Your name is not in our list.';
}
echo '</response>';
echo '<ytag>';
$y = 2;
echo '</ytag>';
echo '<ztag>';
$z = 3;
echo '</ztag>'; ?>
这是JAVASCRIPT文件(namestore.js): -
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("Object not created....");
else
return xmlHttp;
}
function process()
{
if(xmlHttp.readyState==0 || xmlHttp.readyState==4)
{
name = encodeURIComponent(document.getElementById("userInput").value);
xmlHttp.open("GET", "namestore.php?name=" +name, true);
xmlHttp.onreadystatechange = handelResponse;
xmlHttp.send(null);
}
else
{
setTimeout('process()', 1000);
}
}
function handelResponse()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
document.getElementById("outputMsg").innerHTML = xmlResponse.getElementsByTagName("response")[0].childNodes[0].nodeValue;
setTimeout('process()', 1000);
}
else
{
alert("Something is wrong!");
}
}
}
我无法从namestore.js中的namestore.php获取<response>...</response>
标记中的数据。
如何从<response>...</response>
标记中获取数据。
请帮忙......