First, create a public static method in the aspx. cs file, and add the WebMethod attribute.
For example:
[WebMethod]
Public static string GetUserName ()
{
//......
}
If you want to operate the session in this method, you have to set the EnableSession attribute of WebMethod to true. That is:
[WebMethod (EnableSession = true)] // or [WebMethod (true)]
Public static string GetUserName ()
{
//......
}
Then we will write an ajax program to access this program. Let's use jQuery.
$. Ajax ({
Type: "POST ",
ContentType: "application/json ",
Url: "WebForm2.aspx/GetUserName ",
Data :"{}",
DataType: "json ",
Success: function (){.......}
});
Type: Request type. post is required here. The WebMethod only accepts post requests.
ContentType: Content Encoding type when the message is sent to the server. We must use application/json here.
Url: the path of the server-side processing program of the request, in the format of "file name (including suffix)/method name"
Data: list of parameters. Note that the parameters here must be strings in json format. Remember to use the string format, for example, "{aa: 11, bb: 22, cc: 33 ,...}". If you do not write a string, jquery will actually serialize it into a string, so what the server receives is not in json format and cannot be blank, "{}" should be written even if no parameters are available, as shown in the preceding example.
This is why many people fail.
DataType: Data Type returned by the server. It must be json, and none of the others is valid. Because webservice returns data in json format, the format is {"d ":"......."}.
Success: the callback function after the request is successful. You can perform any processing on the returned data here.
The following is an example of an ajax request page for you to test...
Test. aspx
XML/HTML code
<% @ Page language = "C #" %>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e ){
Response. Charset = "gb2312 ";
If (Request. Form ["method"] = "Test") Test ();
Else if (Request. Form ["method"] = "Test1") Test1 ();
Else if (Request. Form ["method"] = "Test2") Test2 ();
Response. Write ("general request <br/> ");
}
Public void Test ()
{
Response. Write ("execute the Test method" + DateTime. Now );
Response. End (); // stop other outputs
}
Public void Test1 ()
{
Response. Write ("execute the Test1 method" + DateTime. Now );
Response. End (); // stop other outputs
}
Public void Test2 ()
{
Response. Write ("execute the Test2 method" + DateTime. Now );
Response. End (); // stop other outputs
}
</Script>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312"/>
<Script type = "text/javascript" src = "jquery. js"> </script>
</Head>
<Body>
<Input type = "button" value = "Call Test" onclick = "CallMethod ('test')"/> <input type = "button" value = "Call Test1"
Onclick = "CallMethod ('test1')"/> <input type = "button" value = "Call Test2" onclick = "CallMethod ('test2')"/>
<Script type = "text/javascript">
Function CallMethod (method ){
$. Ajax (
{
Type: "POST ",
Url: "test. aspx ",
Data: {method: method },
Success: function (msg) {alert (msg );},
Error: function () {alert ('error occurred ');}
}
)
}
$ (Document). ready (function (){
$. Ajax (
{
Type: "POST ",
Url: "test. aspx ",
Data: {method: "Test "},
Success: function (msg) {alert ("$ (document). ready execution method Test returns the Result \ n" + msg );},
Error: function () {alert ('error occurred ');}
}
);
})
</Script>
</Body>
</Html>