jQuery.post()
jQuery.post( url, [ data ], [ success(data, textStatus, jqXHR) ], [ dataType ] ) 返回: jqXHR
描述: 通過服務(wù)器HTTP POST請(qǐng)求加載數(shù)據(jù)
-
version added: 1.0jQuery.post( url, [ data ], [ success(data, textStatus, jqXHR) ], [ dataType ] )
url一個(gè)包含發(fā)送請(qǐng)求的URL字符串
data向服務(wù)器發(fā)送請(qǐng)求的對(duì)象或者字符串參數(shù)
success(data, textStatus, jqXHR)當(dāng)請(qǐng)求成功后執(zhí)行的回調(diào)函數(shù)。
dataType從服務(wù)器返回的預(yù)期的數(shù)據(jù)類型。默認(rèn):智能猜測(cè)(xml, json, script, or html)。
這是一個(gè)快速的AJax處理函數(shù),相當(dāng)于:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success
dataType: dataType
});
success的回調(diào)函數(shù)是根據(jù)MIME類型的響應(yīng),通過返回的數(shù)據(jù)包括XML根節(jié)點(diǎn), 字符串, JavaScript 文件, 或者 JSON 對(duì)象,。 它也通過了響應(yīng)文本狀態(tài)。
在jQuery 1.5,在success回調(diào)函數(shù)還通過了“jqXHR”對(duì)象 ( 在 jQuery 1.4中 ,它是通過XMLHttpRequest對(duì)象)。
大多數(shù)實(shí)現(xiàn)將指定一個(gè)成功的處理函數(shù):
$.post('ajax/test.html', function(data) {
$('.result').html(data);
});
這個(gè)例子所請(qǐng)求的全部HTML代碼片段插在頁面。
用 POST 獲取的頁面是從來不緩存的, 所以這些請(qǐng)求中的 cache 和 ifModified 選項(xiàng)在 jQuery.ajaxSetup() 是沒有效果的。
jqXHR 對(duì)象
在jQuery 1.5,所有jQuery的Ajax方法都返回的超集XMLHTTPRequest對(duì)象。這個(gè)jQuery XHR對(duì)象,或“jqXHR,”通過$.post()約定的接口實(shí)現(xiàn)返回,給它的所有屬性,方法和約定的行為(見Deferred object獲取更多信息)。為了方便和一致性,回調(diào)名稱使用$.ajax(),它提供.error() .success()和.complete()方法。這些方法當(dāng)$.ajax()請(qǐng)求終止時(shí)需要一個(gè)函數(shù)參數(shù)調(diào)用,這個(gè)函數(shù)接收$.ajax()回調(diào)函數(shù)名相同的參數(shù)。
在jQuery 1.5的約定接口也使jQuery的Ajax方法,其中包括$.post() ,以鏈多個(gè).success(), .complete()和.error()回調(diào)的一個(gè)請(qǐng)求,甚至回調(diào)后分配這些請(qǐng)求可能已經(jīng)完成。如果請(qǐng)求已經(jīng)完成,立即觸發(fā)回調(diào)。
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post("example.php", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });
其他注意事項(xiàng):
- 由于瀏覽器的安全限制,大多數(shù)“Ajax”的要求,均采用同一起源的政策 ;該請(qǐng)求不能成功地檢索來自不同的域,子域或協(xié)議的數(shù)據(jù)。
- 如果一個(gè)jQuery.post()請(qǐng)求返回一個(gè)錯(cuò)誤代碼,它會(huì)靜靜的失敗,除非腳本調(diào)用全局的.ajaxError()方法。在jQuery 1.5, 通過jQuery.get()返回的
.error()方法的jqXHR對(duì)象也可用于處理錯(cuò)誤。
Examples:
Example: 請(qǐng)求 test.php 頁面, 但是忽略返回結(jié)果
$.post("test.php");
Example: 請(qǐng)求 test.php 頁面 并且發(fā)送url參數(shù)(雖然仍然忽視返回的結(jié)果)。
$.post("test.php", { name: "John", time: "2pm" } );
Example: 傳遞數(shù)組形式data參數(shù)給服務(wù)器 (雖然仍然忽視返回的結(jié)果)。
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
Example: 使用Ajax請(qǐng)求發(fā)送表單數(shù)據(jù)。
$.post("test.php", $("#testform").serialize());
Example: Alert 從 test.php請(qǐng)求的數(shù)據(jù)結(jié)果 (HTML 或者 XML,取決于返回的結(jié)果)。
$.post("test.php", function(data) {
alert("Data Loaded: " + data);
});
Example: Alert 從 test.cgi請(qǐng)求并且發(fā)送url參數(shù)的數(shù)據(jù)結(jié)果 (HTML 或者 XML,取決于返回的結(jié)果)。
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
alert("Data Loaded: " + data);
});
Example: 得到test.php的內(nèi)容,存儲(chǔ)在一個(gè) XMLHttpResponse 對(duì)象中并且運(yùn)用 process() JavaScript函數(shù)。
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
process(data);
},
"xml"
);
Example: Posts to the test.php page and gets contents which has been returned in json format (<?php echo json_encode(array("name"=>"John","time"=>"2pm")); ?>).
$.post("test.php", { "func": "getNameAndTime" },
function(data){
alert(data.name); // John
console.log(data.time); // 2pm
}, "json");