溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jQuery.post使用的注意事項有哪些

發(fā)布時間:2022-03-05 10:43:56 來源:億速云 閱讀:147 作者:iii 欄目:web開發(fā)

本篇內(nèi)容介紹了“jQuery.post使用的注意事項有哪些”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

  由于瀏覽器的安全限制,大多數(shù)“Ajax”的要求,均采用同一起源的政策 ;即無法從不同的域,子域或協(xié)議中正確接收數(shù)據(jù)。

  如果一個jQuery.post()請求返回一個錯誤代碼,它會靜靜的失敗,除非腳本調(diào)用全局的.ajaxError()方法。在jQuery 1.5, 通過jQuery.post()返回的jqXHR對象的.error()方法也可用于錯誤處理。

  例子:

  Example: 請求 test.php 頁面, 但是忽略返回結(jié)果

  $.post("test.php");

  Example: 請求 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請求發(fā)送表單數(shù)據(jù)。

  $.post("test.php", $("#testform").serialize());

  Example: Alert 從 test.php請求的數(shù)據(jù)結(jié)果 (HTML 或者 XML,取決于返回的結(jié)果)。

  $.post("test.php", function(data) {

  alert("Data Loaded: " + data);

  });

  Example: Alert 從 test.cgi請求并且發(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)容,存儲在一個 XMLHttpResponse 對象中并且運用 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){

  console.log(data.name); // John

  console.log(data.time); //  2pm

  }, "json");

  Example: 用ajax傳遞一個表單并把結(jié)果在一個div中

  <!DOCTYPE html>

  <html>

  <head>

  <script src="https://code.jquery.com/jquery-latest.js"></script>

  </head>

  <body>

  <form action="/" id="searchForm">

  <input type="text" name="s" placeholder="Search..." />

  <input type="submit" value="Search" />

  </form>

  <!-- the result of the search will be rendered inside this div -->

  <div id="result"></div>

  <script>

  /* attach a submit handler to the form */

  $("#searchForm").submit(function(event) {

  /* stop form from submitting normally */

  event.preventDefault();

  /* get some values from elements on the page: */

  var $form = $( this ),

  term = $form.find( 'input[name="s"]' ).val(),

  url = $form.attr( 'action' );

  /* Send the data using post and put the results in a div */

  $.post( url, { s: term },

  function( data ) {

  var content = $( data ).find( '#content' );

  $( "#result" ).empty().append( content );

  }

  );

  });

  </script>

  </body>

  </html>

“jQuery.post使用的注意事項有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI