jQuery ajax - post() 方法

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

執行個體請求 test.php 網頁,忽略傳回值:$.post("test.php");TIY 執行個體通過 AJAX POST 請求改變 div 元素的文本:$("input").keyup(function(){  txt=$("input").val();  $.post("demo_ajax_gethint.asp",{suggest:txt},function(result){    $("span").html(result);  });});親自試一試定義和用法post() 方法通過 HTTP POST 請求從伺服器載入資料。文法jQuery.post(url,data,success(data, textStatus, jqXHR),dataType)參數     描述url     必需。規定把請求發送到哪個 URL。data     可選。映射或字串值。規定連同請求發送到伺服器的資料。success(data, textStatus, jqXHR)     可選。請求成功時執行的回呼函數。dataType     可選。規定預期的伺服器響應的資料類型。預設執行智能判斷(xml、json、script 或 html)。詳細說明該函數是簡寫的 Ajax 函數,等價於:$.ajax({  type: ‘POST‘,  url: url,  data: data,  success: success,  dataType: dataType});根據響應的不同的 MIME 類型,傳遞給 success 回呼函數的返回資料也有所不同,這些資料可以是 XML 根項目、文本字串、JavaScript 檔案或者 JSON 對象。也可向 success 回呼函數傳遞響應的文本狀態。對於 jQuery 1.5,也可以向 success 回呼函數傳遞 jqXHR 對象(jQuery 1.4 中傳遞的是 XMLHttpRequest 對象)。大部分實現會規定一個 success 函數:$.post("ajax/test.html", function(data) {  $(".result").html(data);});本例讀取被請求的 HTML 片段,並插入頁面中。通過 POST 讀取的頁面不被緩衝,因此 jQuery.ajaxSetup() 中的 cache 和 ifModified 選項不會影響這些請求。注釋:由於瀏覽器安全方面的限制,大多數 "Ajax" 請求遵守同源策略;請求無法從不同的域、子域或協議成功地取回資料。注釋:如果由 jQuery.post() 發起的請求返回錯誤碼,那麼不會有任何提示,除非指令碼已調用了全域的 .ajaxError() 方法。或者對於 jQuery 1.5,jQuery.post() 返回的 jqXHR 對象的 .error() 方法也可以用於錯誤處理。jqXHR 對象對於 jQuery 1.5,所有 jQuery 的 AJAX 方法返回的是 XMLHTTPRequest 對象的超集。由 $.post() 返回的 jQuery XHR 對象或 "jqXHR,"實現了約定的介面,賦予其所有的屬性、方法,以及約定的行為。出於對由 $.ajax() 使用的回呼函數名稱便利性和一致性的考慮,它提供了 .error(), .success() 以及 .complete() 方法。這些方法使用請求終止時調用的函數參數,該函數接受與對應命名的 $.ajax() 回呼函數相同的參數。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"); });更多執行個體例子 1請求 test.php 頁面,並一起發送一些額外的資料(同時仍然忽略傳回值):$.post("test.php", { name: "John", time: "2pm" } );例子 2向伺服器傳遞資料數組(同時仍然忽略傳回值):$.post("test.php", { ‘choices[]‘: ["Jon", "Susan"] });例子 3使用 ajax 請求發送表單資料:$.post("test.php", $("#testform").serialize());例子 4輸出來自請求頁面 test.php 的結果(HTML 或 XML,取決於所返回的內容):$.post("test.php", function(data){   alert("Data Loaded: " + data); });例子 5向頁面 test.php 發送資料,並輸出結果(HTML 或 XML,取決於所返回的內容):$.post("test.php", { name: "John", time: "2pm" },   function(data){     alert("Data Loaded: " + data);   });例子 6獲得 test.php 頁面的內容,並儲存為 XMLHttpResponse 對象,並通過 process() 這個 JavaScript 函數進行處理:$.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");

 

jQuery ajax - post() 方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.