求助关于ajax问题
最近终于用get方式实现了ajax无刷新页面显示文字。但是现在要改成post方式却出现了问题,代码如下:html代码(/iknow/text/ajaxtext-1.php文件,因为要实现无刷新登录,所以用php给session赋值,后缀关系应该不大吧):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ajaxtext-1</title>
<script language="javascript">
function createRequest() {
try {
request = new XMLHttpRequest();
}
catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP" );
}
catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP" );
}
catch (faild) {
request = null ;
}
}
}
return request;
}
function onajax(name,age,sex){
var message="name="+name+"&age="+age+"&sex="+sex;
var request=null;
var The_URL;
The_URL="/iknow/text/text-1.php";
alert(The_URL);
/* if(window.XMLHttpRequest){
request=new XMLHttpRequest();
}
else if (window.ActiveXOject){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
alert(request);
*/
request=createRequest();
alert(request);
alert(message);
if(request){
request.open("POST",The_URL,true);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send(message);
alert("1");
request.onReadyStateChange=function(){
if(request.readyState==4 && request.status==200){
if(request.responseText){
document.getElementById("msg").innerHTML=request.responseText;
}
}
}
alert("2");
}else{
alert("please update your browser!");
}
}
</script>
</head>
<body>
<form name="user_info" action="text-1.php" method="post">
姓名:<input type="text" name="name" id="name" /><br />
年龄:<input type="text" name="age" id="age" /><br />
性别:<input type="text" name="sex"id="sex" /><br />
<input type="button" value="提交表单" onClick="onajax(document.getElementById('name').value,document.getElementById('age').value,document.getElementById('sex').value);">
<input type="submit" value="submit" />
</form>
<div id="msg">sdfg</div>
</body>
</html>
php代码(/iknow/text/text-1.php文件,上面的):
<?
header('Content-Type:text/html;charset=gb2312');
echo $_REQUEST[name];
echo "<br/>";
echo $_REQUEST[age];
echo "<br/>";
echo $_REQUEST[sex];
echo "<br/>";
echo $_POST[name];
echo "<br/>";
echo $_POST[age];
echo "<br/>";
echo $_POST[sex];
echo "<br/>";
?>
点击submit跳转text-1.php就可以显示,但是用“提交表单”Button来实现无刷新处理,就发现直接跳过了request.onReadyStateChange。这是为什么?