AJAX中POST請求注意點(header頭)
在進行AJAX請求需要注意POST的區別
在我們進行AJAX請求的過程中,GET請求是簡單可行的.例如
xmlhttp.open("GET","test1.txt",true);
xmlhttp.send();
這是簡單可行的,我們需要把有用的資料拼接到URL後面即可,就像這樣:
xmlhttp.open("GET","demo_get2.asp?fname=Bill&lname=Gates",true);
xmlhttp.send();
但是遇到資料量超過了URL的承載量,我們就需要另外想想辦法了,這個時候,我們要使用post請求
,但是post請求更加複雜,首先具體的格式如下:
xmlhttp.open("POST","demo_post.asp",true);
xmlhttp.send();
但是我們如果想讓他像表單一樣傳送資料需要加上header頭即可:
xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Bill&lname=Gates");
xmlhttp.setRequestHeader(“Content-type”,”application/x-www-form-urlencoded”);注意就是這一行程式碼.