First, create a public static method in the Aspx.cs file, and then add the WebMethod attribute.
Such as:
[WebMethod]
public static string GetUserName ()
{
//......
}
If you want to manipulate the session in this way, you have to set the WebMethod EnableSession property to True. That
[WebMethod (EnableSession = True)]//or [WebMethod (true)]
public static string GetUserName ()
{
//......
}
Then we'll write Ajax programs to access this program, and we'll use jquery.
Copy Code code as follows:
$.ajax ({
Type: "POST",
ContentType: "Application/json",
URL: "Webform2.aspx/getusername",
Data: "{}",
DataType: "JSON",
Success:function () {...}
});
Type: the kind of request, which must be post. The WebMethod method accepts only post type requests.
ContentType: Content encoding type when sending information to the server. We must use Application/json here.
URL: The path of the requested server-side handler, in the form "filename (with suffix)/method name"
Data: Parameter list. Note that the parameter here must be a JSON-formatted string, remembering to be a string format, such as: "{aa:11,bb:22,cc:33, ...}". If you're not writing a string, jquery will actually serialize it into a string, then the server-side is not JSON-formatted, and cannot be empty, even if there are no arguments written as "{}", as in the example above.
A lot of people don't succeed, that's why.
DataType: The data type returned by the server. Must be JSON and none of the others are valid. Because WebService is a JSON format that returns data in the form of: {"D": "..."}.
Success: callback function after a successful request. You can do any processing of the returned data here.
Here is an example of an AJAX request for your own page to test ...
Test.aspx
xml/html Code
Copy Code code as follows:
<%@ 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 test Method" +datetime.now);
Response.End ()//Stop other output
}
public void Test1 ()
{
Response.Write ("Implementation Test1 method" +datetime.now);
Response.End ()//Stop other output
}
public void Test2 ()
{
Response.Write ("Implementation Test2 method" +datetime.now);
Response.End ()//Stop other output
}
</script>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<script type= "Text/javascript" src= "Jquery.js" ></script>
<body>
<input type= "button" value= "Invoke Test" onclick= "Callmethod (' Test ')"/><input type= "button" value= "Call Test1"
onclick= "Callmethod (' Test1 ')"/><input type= button "value=" Calls 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 ');}
}
)
}
$ (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\n\n" +msg);
Error:function () {alert (' Error ');}
}
);
})
</script>
</body>