jquery $.post Usage

Source: Internet
Author: User

Function prototypes: $.post (URL, params, callback)

URL is the address of the submission, eg: "Sample.ashx"

Params is a parameter, eg: {name: "XXX", ID: "001"}

Callback is a callback function, Eg:function (msg) {alert (msg);}

Note 1: In the Sample.ashx section, use the context.request["id" and the context. request["name"] to obtain the value "001" and the value "XXX" respectively, instead of using the context. request.querystring["id"]

Note 2: The function in callback here is triggered after the server returns the value, so there is no need to judge Xmlhttp.readystate==4 && xmlhttp.status==200

Next, let's compare the $.post and the original XMLHTTP method with a piece of code.

For better correspondence, I have 2 ways to implement the same function, passing the same value as the name of the callback function

Code to copy code as follows
/* XMLHTTP Way */

var xmlHttp; Defining Objects XMLHTTP
function createxmlhttprequest ()//Create XMLHttpRequest functions
{
if (window. ActiveXObject)
{
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (window. XMLHttpRequest)
{
XmlHttp = new XMLHttpRequest ();
}
}

function Btn_onclick ()//Assuming a button point has been triggered after this Ajax
{
(www.111cn.net) createxmlhttprequest ();
var url= "Sample.ashx?id=1&name=a"; This is assumed to be passed to Sample.ashx, passing 2 values, id=1 and name=a.
Xmlhttp.open ("POST", url,true);
Xmlhttp.onreadystatechange=response; The callback function is response ()
Xmlhttp.send (NULL);
}

function Response ()
{
if (xmlhttp.readystate==4 && xmlhttp.status==200)
{
alert (Xmlhttp.responsetext); Pop up a box to show what the server is returning
}
}

/* $.post Way */

Code to copy code as follows
function Btn_onclick ()//This is also the event and function, or a point after the trigger
{

/*

Same as Sample.ashx, same as id=1&name=a.
Here the function (msg) is a callback functions, you can write the contents of the execution directly in {}, MSG represents the data returned by the server.
In order to better correspond with the above way, I still let him call response, but need to add parameter msg

*/
$.post ("Sample.ashx", {ID: "1", Name: "A"}, function (msg) {Response (msg);});

}

Code to copy code as follows
function Response (msg)
{
Alert (msg); Pop up a box to show what the server is returning
}



From:http://www.111cn.net/wy/jquery/41928.htm

jquery $.post Usage

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.