test_ajax_post.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ajax_get方式</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="keywords" content="html,css,js,ajax" />
<style type="text/css"> </style>
<script type="text/javascript">
function fun()
{
var name="xioming";
var age="23";
var infor="name="+name+"&age="+age;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","test_ajax_post.php",true);
xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
xmlhttp.send(infor);
}
</script>
</head>
<body>
<h1>ajax_post提交方式</h1>
<div><input type="button" value="点我" onclick="fun()" /></div>
<div id="myDiv"></div>
</body>
</html>
test_ajax_post.php
<?php
header("Content-type:text/html;charset=utf8");
$name=$_POST["name"];
$age=$_POST["age"];
//echo $name." ".$age." hello world";
print_r($_POST);
?>
显示结果: