標籤:
//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方法總結