ajax學習(二)-jquery的ajax方法總結

來源:互聯網
上載者:User

標籤:

//ajax靜態方法-ajax()
//常用ajax屬性總結
$.ajax({
url:"",
async:false,
dataType:"jsonp",//jsonp,json,text,html,script,xml
data: {name:"John",location:"Boston"},
timeout:,
type:,//get or post

username:,
password:,

//事件
beforeSend:function(){},
complete:function(){},
error:function(){},
success:function(){},
dataFilter:function(){},

})

//ajax靜態方法-ajaxSetup(),用來設定預設的ajax屬性,所有屬性都可以設定
//栗子:
$.ajaxSetup({
url: "/xmlhttp/",
global: false,
type: "POST"
});

//ajax靜態方法-get()
//最多四個參數,按照順序分別是:url,data,succese時的function,規定響應的datatype
$.get("test.php",{},function(){

},"json")

//ajax靜態方法-getJSON()
//最多三個參數,按照順序分別是:url,data,succese時的function
$.getJSON("test.json",{},function(){

})

//ajax靜態方法-getScript()
//最多兩個參數,按照順序分別是:url,succese時的function
$.getJSON("test.js",function(){

})



//ajax執行個體方法
/******
ajaxStart(),
ajaxSend(),
ajaxStop(),
ajaxCompleted(),
ajaxError(),
ajaxSuccess()
********/
//栗子
$("div").ajaxStart(function(){

});

//ajax執行個體方法-load()
//最多三個參數,按照順序分別是:url,data,完成時的function
//特別的是,url可以加入選取器如"test.json #container",從而只獲得某部分的內容
/*
function最多有三個參數:
response,返回的結果資料
status,狀態值:success,notmodified,error,timeout,parsererror
xhr,XMLHttpRequest 對象
*/

$("div").load("test.json",{},function(){

})

//進階進階
//ajax靜態方法-param(),序列化一個json對象
//序列化的概念:
//形式上,{ width:1900, height:1200 } ----> width=1680&height=1050
//實際,產生了一個用 encodeURIComponent()編碼的url
//補充:decodeURIComponent(),對 encodeURIComponent()函數編碼成的url進行解碼;
$.param();

//執行個體方法-serialize(),序列化一個表單的值
$("form").serialize()

 

//執行個體方法-serializeArray(),序列化一個表單的值,不同的是返回一個json的對象資料
//返回的格式:[{name:a},{name:b},{name:c}]

$("form").serializeArray()

//補充:each方法 
/*
$.each(a,function(k,v){

})//a為一個json或數組對象,k為序號或稱為下表,v為值

*/

ajax學習(二)-jquery的ajax方法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.