AJAX-POST
POST
- 數據不會出現在地址欄中
- 長度取決於服務器
post.php
<?php
//輸出post請求
echo $_POST['age'];
echo "你好啊"
?>
post.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<button id="btn">點我</button>
</body>
<script>
window.onload = function(){
var btn = document.getElementById("btn");
btn.onclick = function(){
var xhr;
// 1、建立一個 xhr 對象
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject();
}
// 打開一個服務器連接
xhr.open("POST","./post.php",true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
// 3、發送請求
xhr.send("age=88");
// 4、檢測
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
console.log(xhr.responseText);
}
}
}
}
</script>
</html>
效果動態圖