AJAX request $. post method, ajax request. post Method

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.