學(xué)習(xí)Ajax教程,詳細(xì)了解Get與Post
Get方式:
作用:可傳送簡單數(shù)據(jù)
大小:url最大長度是2083 bytes,可以用于GET傳遞數(shù)據(jù)的長度是2048 bytes
包含體:數(shù)據(jù)追加到url中發(fā)送,也就是http的header傳送
Post方式:
作用:可傳送簡單復(fù)雜數(shù)據(jù)
大小:web.config里限制
包含體:數(shù)據(jù)在http請(qǐng)求的實(shí)體內(nèi)容里傳送
Ajax用Post模式傳送數(shù)據(jù).需注意:
1.設(shè)置header的Context-Type為application/x-www-form-urlencode確保服務(wù)器知道實(shí)體中有參數(shù)變量.通常使用XmlHttpRequest對(duì)象的
SetRequestHeader("Context-Type","application/x-www-form-urlencoded;")
2.參數(shù)是名/值一一對(duì)應(yīng)的鍵值對(duì),每對(duì)值用&號(hào)隔開.如 name=abc&sex=man&age=18.
3.參數(shù)在Send(參數(shù))方法中發(fā)送
4.服務(wù)器端請(qǐng)求參數(shù)區(qū)分Get與Post.例如asp.net中以Request.Form["name"]對(duì)實(shí)體中的參數(shù)請(qǐng)求.這時(shí)url參數(shù)請(qǐng)求Request.QueryString["name"]將引發(fā)異常
function CreateXmlHttp()
{
if(window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
return new XmlHttpRequest();
}
}
//---------------------------------------------------------------------------------------------------------------------
//post方式:
function Start()
{
var ParamString = "name=abc&sex=man&age=18";
var XmlHttpObject = CreateXmlHttp();
XmlHttpObject.onreadystatechange = StateEvent;
XmlHttpObject.open("post","test.aspx",true);
XmlHttpObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); //設(shè)置服務(wù)器響應(yīng)請(qǐng)求體參數(shù)
XmlHttpObject.send(ParamString);
}
//---------------------------------------------------------------------------------------------------------------------
//get方式:
function Start()
{
var XmlHttpObject = CreateXmlHttp();
XmlHttpObject.onreadystatechange = StateEvent;
XmlHttpObject.open("get",url,true);
XmlHttpObject.send(null);
}
//---------------------------------------------------------------------------------------------------------------------
//創(chuàng)建回調(diào)函數(shù)
function StateEvent()
{
if(http_request.readyState==4)//服務(wù)器響應(yīng)狀態(tài)
{
if(http_request.status==200|| http_request.status == 304)//代碼執(zhí)行狀態(tài)
{
var resStr=http_request.responseText;
alert(resStr);//這里再處業(yè)務(wù)邏輯處理
}
else
{
alert("您所請(qǐng)求的頁面有異常!");
}
}
else
{
selectedResTits.innerHTML="<img src='images/wjb/icons/indicator.gif' alt=''/>";
}
}
</script>