標籤:發送 支援 get 通過 state cat ... class 發送post請求
AJAX:Asynchronous Javascript and xml 非同步,Js和Xml 互動式網頁開發 不重新整理頁面,與伺服器互動 詳情請參照Jquery工具指南
用在瀏覽器端的技術,無重新整理,通過XmlHttpRequest訪問頁面
純js版----------
if(XmlHttpRequest){ //判斷是否支援XmlHttpRequestxhr= new XmlHttpRequest(); // 建立XmlHttpRequest對象}else{xhr= new Activexobject("Microsoft.XMLHTTP"); //不支援XmlHttpRequest,使用此方法建立 }xhr.open("get|post","url",true);xhr.send(); // 開始發送xhr.onreadystatechange= function(){ // 回呼函數,當伺服器將資料返回給瀏覽器觸發方法if(xhr.readyState==4) //0沒調用open方法,1表示未調用send方法,2正在等待狀態代碼和頭的返回,3 已接受部分資料,但還沒接受完,不能使用該對象的屬性和方法,4已載入,所有資料執行完畢if(xhr.status==200){ // 響應狀態代碼,表示頁面執行無誤alert(xhr.responseText); // 輸出接受到的文本}}
發送post請求
在send中寫資料。並添加請求前序
如
xhr.setRequestHeader("Content-Type",application/x-www-form-urlencoded)xhr.send("id=123&pwd=456");
-------------------------------------
Jquery版
強大的Jquery。。只需要get頁面就夠了
$.get("url",{"id" : "123","pwd":456},function(data)){ //自動把參數當做get請求傳輸alert(data)}
只需要post頁面。
$.post("url",{"id" : 123,"pwd":456},function(data)){ //post請求alert(data)}
//第三種寫法
$.ajax({type="get"| post,url="...",data:"參數",success:function(msg){...} // msg為從伺服器接受到的資料})
Ajax,純Js+Jquery