## POST 請求
一個簡單`POST`請求:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function loadXMLDoc()
{
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","/statics/demosource/demo_post.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
```
如果需要像 HTML 表單那樣 POST 數據,請使用`setRequestHeader()`來添加 HTTP 頭。然后在`send()`方法中規定您希望發送的數據:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function loadXMLDoc()
{
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","/statics/demosource/demo_post2.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
```
| 方法 | 描述 |
| :-- | :-- |
| setRequestHeader(*header,value*) | 向請求添加 HTTP 頭。*header*: 規定頭的名稱; *value*: 規定頭的值|
提示:`POST`請求的特點如下:
* `POST`請求不會被緩存
* `POST`請求不會保留在瀏覽器歷史記錄中
* `POST`請求不能被收藏為書簽
* `POST`請求對數據長度沒有要求
## url - 服務器上的文件
* * *
`open()`方法的`url`參數是服務器上文件的地址:
~~~
xmlhttp.open("GET","ajax_test.html",true);
~~~
該文件可以是任何類型的文件,比如`.txt`和`.xml`,或者服務器腳本文件,比如`.asp`和`.php`(在傳回響應之前,能夠在服務器上執行任務)。
## 異步 - True 或 False?
* * *
AJAX 指的是異步[JavaScript](https://www.w3cschool.cn/javascript/js-intro.html)和 XML(Asynchronous JavaScript and XML)。
XMLHttpRequest 對象如果要用于 AJAX 的話,其`open()`方法的`sync`參數必須設置為 true:
~~~
xmlhttp.open("GET","ajax_test.html",true);
~~~
對于 web 開發人員來說,發送異步請求是一個巨大的進步。很多在服務器執行的任務都相當費時。AJAX 出現之前,這可能會引起應用程序掛起或停止。
通過 AJAX,JavaScript 無需等待服務器的響應,而是:
* 在等待服務器響應時執行其他腳本
* 當響應就緒后對響應進行處理
## Async=true
當使用 async=true 時,請規定在響應處于 onreadystatechange 事件中的就緒狀態時執行的函數:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tryrun 1</title>
</head>
<body>
<div id="view">
<p>點擊下面的按鈕,將 Ajax 請求回來的數據更新在該文本內</p>
</div>
<button type="button" id="btn">發起 Ajax 請求</button>
<script>
document.getElementById("btn").onclick = ajaxRequest;
function ajaxRequest () {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.w3cschool.cn/statics/demosource/ajax_info.txt", true);
xhr.send();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("view").innerHTML = xhr.responseText;
}
}
}
</script>
</body>
```
## Async = false
* * *
如需使用 async=false,請將`open()`方法中的第三個參數改為 false:
~~~
xmlhttp.open("GET","test1.txt",false);
~~~
我們不推薦使用 async=false,但是對于一些小型的請求,也是可以的。
請記住,JavaScript 會等到服務器響應就緒才繼續執行。如果服務器繁忙或緩慢,應用程序會掛起或停止。
注意:當您使用 async=false 時,請不要編寫 onreadystatechange 函數 - 把代碼放到`send()`語句后面即可:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function loadXMLDoc()
{
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.open("GET","/statics/demosource/ajax_info.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
```