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