Juery Ajax calls the WebMethod method in the Aspx.cs page: first in the Aspx.cs file to build a public static method, and then add the WebMethod attribute, the specific implementation of the following, interested friends can refer to ha, I hope to help you all
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,eve Ntargs 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 ("Execute Test1 Method" +datetime.now);
Response.End ();//Stop other output
}
public void Test2 ()
{
Response.Write ("Execute 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>
</ 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 ');}
}
)
}
$ (document). Ready (function () {
$.ajax (
{
Type: ' POST ',
URL: ' test.aspx ',
Data:{method: "Test"},
Success:function (msg) {alert ("$ (document). Ready execution Method Test Returns the result nnn" +msg);
Error: function () {alert (' Error ');}
}
);
})
</script>
</body>