jQuery Ajax POST方法

Sends an asynchronous http POST request to load data from the server. Its general form is:

發送異步http POST請求以從服務器加載數據。 其一般形式為:

jQuery.post( url [, data ] [, success ] [, dataType ] )
  • url : is the only mandatory parameter. This string contains the adress to which to send the request. The returned data will be ignored if no other parameter is specified

    url:是唯一的必需參數。 此字符串包含向其發送請求的地址。 如果未指定其他參數,則將忽略返回的數據
  • data : A plain object or string that is sent to the server with the request.

    data:與請求一起發送到服務器的普通對象或字符串。
  • success : A callback function that is executed if the request succeeds.it takes as an argument the returned data. It is also passed the text status of the response.

    成功:如果請求成功執行的回調函數,它將返回的數據作為參數。 它還傳遞了響應的文本狀態。
  • dataType : The type of data expected from the server. The default is Intelligent Guess (xml, json, script, text, html). if this parameter is provided, then the success callback must be provided as well.

    dataType:服務器期望的數據類型。 默認值為Intelligent Guess(xml,json,腳本,文本,html)。 如果提供了此參數,則還必須提供成功回調。

例子 (Examples)

$.post('http://example.com/form.php', {category:'client', type:'premium'});

requests form.php from the server, sending additional data and ignoring the returned result

從服務器請求form.php ,發送其他數據并忽略返回的結果

$.post('http://example.com/form.php', {category:'client', type:'premium'}, function(response){ alert("success");$("#mypar").html(response.amount);
});

requests form.php from the server, sending additional data and handling the returned response (json format). This example can be written in this format:

從服務器請求form.php ,發送其他數據并處理返回的響應(json格式)。 該示例可以用以下格式編寫:

$.post('http://example.com/form.php', {category:'client', type:'premium'}).done(function(response){alert("success");$("#mypar").html(response.amount);
});

The following example posts a form using Ajax and put results in a div

以下示例使用Ajax發布表單并將結果放入div中

<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>jQuery.post demo</title><script src="https://code.jquery.com/jquery-1.10.2.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 normallyevent.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 postvar posting = $.post( url, { s: term } );// Put the results in a divposting.done(function( data ) {var content = $( data ).find( "#content" );$( "#result" ).empty().append( content );});
});
</script></body>
</html>

The following example use the github api to fetch the list of repositories of a user using jQuery.ajax() and put results in a div

以下示例使用github API使用jQuery.ajax()獲取用戶的存儲庫列表,并將結果放入div中

<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>jQuery Get demo</title><script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body><form id="userForm"><input type="text" name="username" placeholder="Enter gitHub User name"><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
$( "#userForm" ).submit(function( event ) {// Stop form from submitting normallyevent.preventDefault();// Get some values from elements on the page:var $form = $( this ),username = $form.find( "input[name='username']" ).val(),url = "https://api.github.com/users/"+username+"/repos";// Send the data using postvar posting = $.post( url, { s: term } );//Ajax Function to send a get request$.ajax({type: "GET",url: url,dataType:"jsonp"success: function(response){//if request if made successfully then the response represent the data$( "#result" ).empty().append( response );}});});
</script></body>
</html>

jQuery.ajax() (jQuery.ajax())

$.post( url [, data ] [, success ] [, dataType ] ) is a shorthand Ajax function, equivalent to:

$.post( url [, data ] [, success ] [, dataType ] )是Ajax的簡寫功能,等效于:

$.ajax({type: "POST",url: url,data: data,success: success,dataType: dataType
});

$.ajax() provides way more options that can be found here

$.ajax()提供了更多可以在此處找到的選項

更多信息: (More Information:)

For more information, please visit the official website

欲了解更多信息,請訪問官方網站

翻譯自: https://www.freecodecamp.org/news/jquery-ajax-post-method/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390838.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390838.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390838.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

spss23出現數據消失_改善23億人口健康數據的可視化

spss23出現數據消失District Health Information Software, or DHIS2, is one of the most important sources of health data in low- and middle-income countries (LMICs). Used by 72 different LMIC governments, DHIS2 is a web-based open-source platform that is used…

01-hibernate注解:類級別注解,@Entity,@Table,@Embeddable

Entity Entity:映射實體類 Entity(name"tableName") name:可選&#xff0c;對應數據庫中一個表&#xff0c;若表名與實體類名相同&#xff0c;則可以省略。 注意&#xff1a;使用Entity時候必須指定實體類的主鍵屬性。 第一步&#xff1a;建立實體類&#xff1a; 分別…

leetcode 1707. 與數組中元素的最大異或值

題目 給你一個由非負整數組成的數組 nums 。另有一個查詢數組 queries &#xff0c;其中 queries[i] [xi, mi] 。 第 i 個查詢的答案是 xi 和任何 nums 數組中不超過 mi 的元素按位異或&#xff08;XOR&#xff09;得到的最大值。換句話說&#xff0c;答案是 max(nums[j] XO…

MySQL基礎入門學習【2】數據類型

數據類型&#xff1a;指列、存儲過程參數、表達式和局部變量的數據特征&#xff0c;它決定了數據的存儲格式&#xff0c;代表了不同的信息類型 &#xff08;1&#xff09; 整型(按存儲范圍分類)&#xff1a;TINYINT&#xff08;1字節&#xff09; SAMLLINT&#xff08;2字節&am…

昆西·拉森的凈資產是多少?

People ask me how much I get paid all the time. It comes up on podcast interviews, Quora questions, and face-to-face discussions.人們問我&#xff0c;我一直得到多少報酬。 它來自播客訪談&#xff0c;Quora問題和面對面的討論。 And people search this question a…

COVID-19研究助理

These days scientists, researchers, doctors, and medical professionals face challenges to develop answers to their high priority scientific questions.如今&#xff0c;科學家&#xff0c;研究人員&#xff0c;醫生和醫學專家面臨著挑戰&#xff0c;無法為其高度優先…

Node.js umei圖片批量下載Node.js爬蟲1.00

這個爬蟲在abaike爬蟲的基礎上改改圖片路徑和下一頁路徑就出來了&#xff0c;代碼如下&#xff1a; // // umei圖片批量下載Node.js爬蟲1.00 // 2017年11月13日 //// 內置http模塊 var httprequire("http");// 內置文件處理模塊&#xff0c;用于創建目錄和圖片文件 v…

交通銀行信息技術管理部副總經理張漫麗:交通銀行“大數據+人工智能”應用研究...

文 | 交通銀行信息技術管理部副總經理張漫麗 大數據隱含著巨大的社會、經濟、科研價值&#xff0c;已引起了各行各業的高度重視。如果能通過人工智能技術有效地組織和使用大數據&#xff0c;將對社會經濟和科學研究發展產生巨大的推動作用&#xff0c;同時也孕育著前所未有的機…

安軟件一勞永逸_如何克服一勞永逸地公開演講的恐懼

安軟件一勞永逸If you’re like most people, the idea of public speaking terrifies you (it terrifies me too). So how do you get over those jitters, get up on stage, and give an amazing talk? First, a disclaimer: this article is purely about your stage prese…

Go語言實戰 : API服務器 (8) 中間件

為什么需要中間件 我們可能需要對每個請求/返回做一些特定的操作&#xff0c;比如 記錄請求的 log 信息在返回中插入一個 Header部分接口進行鑒權 這些都需要一個統一的入口。這個功能可以通過引入 middleware 中間件來解決。Go 的 net/http 設計的一大特點是特別容易構建中間…

缺失值和異常值的識別與處理_識別異常值-第一部分

缺失值和異常值的識別與處理&#x1f4c8;Python金融系列 (&#x1f4c8;Python for finance series) Warning: There is no magical formula or Holy Grail here, though a new world might open the door for you.警告 &#xff1a; 這里沒有神奇的配方或圣杯&#xff0c;盡管…

SQL Server 常用分頁SQL

今天無聊和朋友討論分頁&#xff0c;發現網上好多都是錯的。網上經常查到的那個Top Not in 或者Max 大部分都不實用&#xff0c;很多都忽略了Order和性能問題。為此上網查了查&#xff0c;順帶把2000和2012版本的也補上了。 先說說網上常見SQL的錯誤或者說局限問題 12345select…

Word中摘要和正文同時分欄后,正文跑到下一頁,怎么辦?或Word分欄后第一頁明明有空位后面的文字卻自動跳到第二頁了,怎么辦?...

問題1&#xff1a;Word中摘要和正文同時分欄后&#xff0c;正文跑到下一頁&#xff0c;怎么辦&#xff1f;或Word分欄后第一頁明明有空位后面的文字卻自動跳到第二頁了&#xff0c;怎么辦&#xff1f; 答&#xff1a;在word2010中&#xff0c;菜單欄中最左側選“文件”->“選…

leetcode 664. 奇怪的打印機(dp)

題目 有臺奇怪的打印機有以下兩個特殊要求&#xff1a; 打印機每次只能打印由 同一個字符 組成的序列。 每次可以在任意起始和結束位置打印新字符&#xff0c;并且會覆蓋掉原來已有的字符。 給你一個字符串 s &#xff0c;你的任務是計算這個打印機打印它需要的最少打印次數。…

SQL數據類型說明和MySQL語法示例

SQL數據類型 (SQL Data Types) Each column in a database table is required to have a name and a data type. 數據庫表中的每一列都必須具有名稱和數據類型。 An SQL developer must decide what type of data that will be stored inside each column when creating a tab…

PHP7.2 redis

為什么80%的碼農都做不了架構師&#xff1f;>>> PHP7.2 的redis安裝方法&#xff1a; 順便說一下PHP7.2的安裝&#xff1a; wget http://cn2.php.net/distributions/php-7.2.4.tar.gz tar -zxvf php-7.2.4.tar.gz cd php-7.2.4./configure --prefix/usr/local/php…

leetcode 1787. 使所有區間的異或結果為零

題目 給你一個整數數組 nums??? 和一個整數 k????? 。區間 [left, right]&#xff08;left < right&#xff09;的 異或結果 是對下標位于 left 和 right&#xff08;包括 left 和 right &#xff09;之間所有元素進行 XOR 運算的結果&#xff1a;nums[left] XOR n…

【JavaScript】網站源碼防止被人另存為

1、禁示查看源代碼 從"查看"菜單下的"源文件"中同樣可以看到源代碼&#xff0c;下面我們就來解決這個問題&#xff1a; 其實這只要使用一個含有<frame></frame>標記的網頁便可以達到目的。 <frameset> <frame src"你要保密的文件…

梯度 cv2.sobel_TensorFlow 2.0中連續策略梯度的最小工作示例

梯度 cv2.sobelAt the root of all the sophisticated actor-critic algorithms that are designed and applied these days is the vanilla policy gradient algorithm, which essentially is an actor-only algorithm. Nowadays, the actor that learns the decision-making …

共享語義 unix語義_語義UI按鈕

共享語義 unix語義什么是語義UI按鈕&#xff1f; (What are Semantic UI Buttons?) A button indicates a possible user action. Semantic UI provides an easy-to-use syntax that simplifies not only the styling of a button, but also the natural language semantics.按…