函數:$.ajax(properties)
功能:用一個http請求訪問一個遠程頁面
返回:XMLHttpRequest
參數;
(String)url請求地址,
(String)type請求類型("GET","POST")
(String)dataType:從伺服器端返回的資料類型("xml","html","script","json")
(Boolean)ifModified:根據Last-Modified header判斷返回內容是否改變
(Number)timeout:請求逾時
(Boolean)global:對此次請求是否引起全域的ajax事件出來,預設true
(Function)error:錯誤處理函數
(Function)complete:請求完成後的處理函數
(Object|String)data:發送到伺服器端的資料
(String) contentType :預設"application/x-www-form-urlencoded"
(Boolean) processData :傳輸到伺服器端的資料預設被轉換到query string中以適合預設"application/x-www-form-urlencoded"
方式,如果你想以DOMDocuments方式傳輸資料,就將該選項設為false
(Boolean)aysnc:是否非同步傳輸,預設為true
(Function)beforeSend:請求前響應函數
例子:
Load and execute a JavaScript file.
jQuery Code
$.ajax({
type: "GET",
url: "test.js",
dataType: "script"
})
Save some data to the server and notify the user once its complete.
jQuery Code
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
jQuery Code
var html = $.ajax({
url: "some.php",
async: false
}).responseText;
jQuery Code
var xmlDocument = [create xml document];
$.ajax({
url: "page.php",
processData: false,
data: xmlDocument,
success: handleResponse
});
函數:$.ajaxSetup(settings),$.ajaxTimeout(time)
功能:設定請求的一些參數
返回:undefined
例子:
$.ajaxSetup( {
url: "/xmlhttp/",
global: false,
type: "POST"
} );
$.ajaxTimeout( 5000 );
函數:$.get(url, params, callback),$.getIfModified(url, params, callback),
$.getJSON(url, params, callback),$.getScript(url, callback)
功能:get方式提交資料
例子:
$.get("test.cgi",
{ name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
}
);
函數:$.post(url, params, callback)
功能:post方式提交資料
例子:
$.post("test.cgi",
{ name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
}
);
函數:ajaxComplete(callback),ajaxComplete(callback),ajaxSend(callback)
ajaxStart(callback),ajaxStop(callback),ajaxSuccess(callback)
功能:XMLHttpRequest狀態改變過程中各個響應處理函數
例子:
$("#msg").ajaxComplete(function(request, settings){
$(this).append("<li>Request Complete.</li>");
});
$("#msg").ajaxSuccess(function(request, settings){
$(this).append("<li>Successful Request!</li>");
});
$("#loading").ajaxStop(function(){
$(this).hide();
});
$("#msg").ajaxSend(function(request, settings){
$(this).append("<li>Starting request at " + settings.url + "</li>");
});
函數:load(url, params, callback),loadIfModified(url, params, callback)
功能:載入html內容
返回:jQuery對象
參數:同get和post方式提交
例子:
jQuery Code
$("#feeds").load("feeds.html");
Before
<div id="feeds"></div>
Result:
<div id="feeds"><b>45</b> feeds found.</div>
jQuery Code
$("#feeds").load("feeds.html",
{limit: 25},
function() { alert("The last 25 entries in the feed have been loaded"); }
函數:serialize()
功能:將表單元素和值序列化成string
返回:String
例子:
jQuery Code
$("input[@type=text]").serialize();
Before
<input type='text' name='name' value='John'/>
<input type='text' name='location' value='Boston'/
Result
name=John&location=Boston