跪求ajax问题

WBOY
WBOY원래의
2016-06-23 13:43:35891검색

<!DOCUTYPE html><html>	<head>		<title>聊天窗口</title>		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">	</head>	<script type="text/javascript">	 window.resizeTo(580,650);	 window.setInterval("getMessage()", 3000);	 	 function getXmlHttpObject(){		var xmlHttpRequest;		//不同的浏览器获取对象xmlhttprequest对象方法不一样		if(window.ActiveXObject){			xmlHttpRequest =new ActiveXObject("Microsoft.XMLHTTP");		}else{			xmlHttpRequest =new XMLHttpRequest();		}		return xmlHttpRequest;	}	function getMessage()	{		var myXmlHttpRequest = getXmlHttpObject();	 	if(myXmlHttpRequest)	 	{	 		var url = "getMessage.php";	 		var data = "getter=<?php @session_start(); echo $_SESSION['username'];?>&sender=<?php echo $_GET['username'];?>";	 		myXmlHttpRequest.open("post",url,true);	 		myXmlHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");	 		myXmlHttpRequest.onreadystatechange=function()	 		{		 		if(myXmlHttpRequest.readyState==4)		 		{		 			if(myXmlHttpRequest.status==200)		 			{		 				//接收		 				var mes=myXmlHttpRequest.responseXML;		 						 				window.alert(mes);  //这里打印出来老是为null		 			}		 		}	 		}	 		myXmlHttpRequest.send(data);	 	}	}	 function sendMessage()	 {	 	var myXmlHttpRequest = getXmlHttpObject();	 	if(myXmlHttpRequest)	 	{	 		var url = "sendMessage.php";	 		var data = "con=" + $('con').value +"&getter=<?php echo $_GET['username'];?>"+"&sender=<?php @session_start(); echo $_SESSION['username'];?>";		 	myXmlHttpRequest.open("post",url,true);		 	myXmlHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");		 	myXmlHttpRequest.onreadystatechange=function()		 	{		 		if(myXmlHttpRequest.readyState==4)		 		{		 			if(myXmlHttpRequest.status==200)		 			{		 			}		 		}		 	}		 	myXmlHttpRequest.send(data);	 	}		 }	 function $(id)	 {		return document.getElementById(id);	 }	</script>	<body>		<center>			<h2><span style="color:red;"><?php @session_start(); echo $_SESSION['username'];?></span>跟<span style="color:red;"><?php echo $_GET['username'];?></span>聊天</h2>			<textarea name="context" rows="20" cols="70"></textarea><br/>			<input style="width:300px" type="text" name="sendcontext" id="con">			<input type="button" name="send" onclick="sendMessage();" value="发送">		</center>	</body></html>

上面的getMessage()函数是获取ajax查找出来的数据,getMessage()函数里面的.onreadystatechange指定的回调函数中window.alert(mes);打印出来老是为空,但是我ajax请求的getMessage页面有返回数据啊,下面是ajax请求getMessage的代码
<?php	include "MessageServer.class.php";	@header( 'Content-Type:text/xml;charset=utf-8'); 	@header("Cache-Control: no-cache");	$getter = $_POST['getter'];	$sender = $_POST['sender'];	//调用MessageSerive获取信息	$messageServer = new MessageServer();	$messList = $messageServer->getMessage($getter, $sender);	echo $messList;	?>

echo $messLis t是有数据输出的,输出的是


回复讨论(解决方案)

                 //接收
                 var mes=myXmlHttpRequest.responseXML;
if(mes.xml == '') {
alert(myXmlHttpRequest.responseText);
return;
}
               window.alert(mes);  //这里打印出来老是为null

是数据流,你没有定义输入的内容格式 ,可以是json或数据 

 if(myXmlHttpRequest.status==200) {
if(myXmlHttpRequest.recvType=="XML")
     echo  myXmlHttpRequest.responseXML;
 }

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.