jQuery+ajax操作大全
參數 描述
url 必需。規定把請求發送到哪個 URL。
data 可選。映射或字符串值。規定連同請求發送到服務器的數據。
success(data, textStatus, jqXHR) 可選。請求成功時執行的回調函數。
dataType可選。規定預期的服務器響應的數據類型。
默認執行智能判斷(xml、json、script 或 html)。
jQuery.post(url,data,success(data, textStatus, jqXHR),dataType);
$.ajax({
type:
'POST'
,
url: url,
data: data,
success: success,
dataType: dataType
});
大部分實現會規定一個 success 函數://該函數是簡寫的 Ajax 函數,等價於:
$.post(
"ajax/test.html"
,
function
(data) {
$(
".result"
).html(data);
});
//jQuery 1.5 中的約定接口同樣允許 jQuery 的 Ajax 方法,包括 $.post(),來鏈接同一請求的多個 .success()、.complete() 以及 .error() 回調函數,甚至會在請求也許已經完成後分配這些回調函數。
// 請求生成後立即分配處理程序,請記住該請求針對 jqxhr 對象
var
jqxhr = $.post(
"example.php"
,
function
() {
alert(
"success"
);
})
.success(
function
() { alert(
"second success"
); })
.error(
function
() { alert(
"error"
); })
.complete(
function
() { alert(
"complete"
); });
// 爲上面的請求設置另一個完成函數
jqxhr.complete(
function
(){ alert(
"second complete"
); });
$(
"input"
).keyup(
function
(){
txt=$(
"input"
).val();
$.post(
"/jquery/gethint.asp"
,{suggest:txt},
function
(result){
$(
"span"
).html(result);
});
});
//通過 AJAX POST 請求改變 div 元素的文本:
//例子 1
//請求 test.php 頁面,並一起發送一些額外的數據(同時仍然忽略返回值):
1
|
$.post( "test.php" , { name: "John" , time: "2pm" } ); |
//例子 2
//向服務器傳遞數據數組(同時仍然忽略返回值):
$.post(
"test.php"
, {
'choices[]'
: [
"Jon"
,
"Susan"
] });
//使用 ajax 請求發送表單數據:///例子 3
$.post(
"test.php"
, $(
"#testform"
).serialize());
//輸出來自請求頁面 test.php 的結果(HTML 或 XML,取決於所返回的內容)://例子 4
$.post(
"test.php"
,
function
(data){
alert(
"Data Loaded: "
+ data);
});
//向頁面 test.php 發送數據,並輸出結果(HTML 或 XML,取決於所返回的內容)://例子 5
$.post(
"test.php"
, { name:
"John"
, time:
"2pm"
},
function
(data){
alert(
"Data Loaded: "
+ data);
});
//獲得 test.php 頁面的內容,並存儲爲 XMLHttpResponse 對象,並通過 process() 這個 JavaScript 函數進行處理://例子 6
$.post( "test.php" , { name: "John" , time: "2pm" }, function (data){ process(data); }, "xml" ); |
//例子 7
//獲得 test.php 頁面返回的 json 格式的內容:
$.post( "test.php" , { "func" : "getNameAndTime" }, function (data){ alert(data.name); // John console.log(data.time); // 2pm }, "json" ); |
$( "#province" ).change( function (){ var pid = $( "#province option:selected" ).val(); var href= '/common/ajax_scenic.php?action=city&pid=' +pid+ '&' +Math.random(); var params = { type: 'GET' , url:href, dataType: 'text' , success: function (data){ jQuery( "#city" ).append(data); } }; jQuery.ajax( params ); }); |
$.ajax({ url: '/member/ajax.php' +Math.random(), dataType: 'text' , data:encodeURI( 'action=edit_phone&uid=' +uid+ '&phone=' +phone), success: function (data){ var jsondata = eval( '(' +data+ ')' ); if (jsondata.result== 'failure' ){ alert(jsondata.content); } if (jsondata.result== 'success' ){ $( '#comment_list' ).after(jsondata.html); alert( '評論成功!' ); } } }); |
var params = { type: 'GET' , url:href, dataType: 'text' , success: function (data){ jQuery( "#city" ).append(data); //$(".test").hide(); } }; jQuery.ajax( params ); |
//完整的AJAX代碼
$.ajax({ type: 'GET' , url:href, dataType: 'text' , data:encodeURI( 'id=1&uid=' +uid), beforeSend: function (){ $( "#msg" ).html( "logining" ); }, success: function (data){ jQuery( "#city" ).append(data); }, complete: function (XMLHttpRequest, textStatus){ alert(XMLHttpRequest.responseText);alert(textStatus); }, error: function (){} , }); |
$( '#myform' ).ajaxForm({ beforeSubmit: checkForm, success: complete, dataType: 'json' }); |