function addURLParam(url, name, value){
url += (url.indexOf("?") == -1 ? "?" : "&");
url += encodeURIComponent(name) + "=" + encodeURIComponent(value);
return url;
}
var url = "test.php";
//添加参数
url = addURLParam(url, "name", "Nicholas");
url = addURLParam(url, "hometown", "shanghai");
//初始化请求
xhr.open("get", url, false);
xhr.open("post", "test.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var form = document.getElementById("user");
//serialize()序列化表单数据
xhr.send(serialize(form));
<?php
//设置头部信息,发送给服务器的数据出如今$_POST超级全局变量中。
//不然,要访问一样的数据,要用$HTTP_RAW_POST_DATA
header("Content-Type: text/plain");
echo <<<EOF
Name: {$_POST["username"]}
EOF;
?>