Use juery's ajax method to call the webmethod method on the aspx. cs page

Source: Internet
Author: User
First, create a public static method in the aspx. cs file, and add the WebMethod attribute. For example: [WebMethod] publicstaticstringGetUserName () {//...} if you want to operate the session in this method, you must EnableSession of WebMethod...

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 #" %>

Related Article

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.