AJAX request $. post method, ajax request. post Method
The $. post method of jQuery can be used to initiate AJAX requests to the server in the form of POST. $. The post method is a practical tool of jQuery.
$. Post method syntax
$. Post (url, parameters, callback) |
Parameters |
|
Url |
(String) The server resource address. |
Parameter |
(Object) parameters must be passed to the server. The parameter format is "key/value ". |
Callback |
(Function) is called when the request is complete. The function parameters are the response body and status in sequence. |
Return Value |
XHR instance |
Let's look at a simple example.
Client code:
1234567891011121314151617181920212223 |
< html xmlns="http://www.w3.org/1999/xhtml"> < head > < title ></ title > < script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></ script > < script type="text/javascript"> $().ready(function () { $('#selectNum').change(function () { var idValue = $(this).val(); // Use the POST method to call the service $.post('Server.aspx', { id: idValue }, function (text, status) { alert(text); }); }) }) </ script > </ head > < body > < select id="selectNum"> < option value="0">--Select--</ option > < option value="1">1</ option > < option value="2">2</ option > < option value="3">3</ option > </ select > </ body > </ html > |
Main server code:
12345678910111213141516171819202122232425262728293031 |
protected void Page_Load( object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request[ "id" ] != null && ! string .IsNullOrEmpty(Request[ "id" ].ToString())) { Response.Write( GetData(Request[ "id" ].ToString())); } } } protected string GetData( string id) { string str = string .Empty; switch (id) { case "1" : str += "This is Number 1" ; break ; case "2" : str += "This is Number 2" ; break ; case "3" : str += "This is Number 3" ; break ; default : str += "Warning Other Number!" ; break ; } return str; } |
Run the program and Result
Use httpwatcher to intercept request information. If you select a number from the drop-down list, you can intercept the following request information.
When the $. post method is used:
We can see that there are parameters in POST Data, indicating that this is a POST request.
On-Server StatusChanged, OrModify updatesSome data uses POST requests.