XMLHttpRequest 可以Get,也可以Post向服務器傳遞參數
var xhr = new XMLHttpRequest();
$(document).ready(function(){
$("button").click(function(){
var paras = 'firstname=zhang&lastname=san';
//Get 方式,直接將參數放到URL鏈接中
xhr.open("get","God.asp?firstname=zhang&lastname=san&toGod=Gold");
xhr.send(null);
//Post 方式,參數放在單獨的請求中
xhr.open("post","God.asp");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
xhr.send('firstname=zhang&lastname=san');
如果Post方式,需要指定內容的格式:
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
后台使用ASP
<%
dim fn
fn = Request.querystring("firstname")
if (fn = "") then
fn = Request.Form("firstname")
end if
dim ln
ln = Request.querystring("lastname")
if (ln = "") then
fn = Request.Form("lastname")
end if
%>
對於Get方式傳參, 使用Request.querystring();
而是用Post方式傳參,使用Request.Form()
當然,POST傳遞參數,有四種方式,
form-data、x-www-form-urlencoded、raw、binary
有興趣的同學,可以再研究一下。