JQuery Ajax Request Aspx backend method

Source: Internet
Author: User

It is still convenient to use jquery encapsulated Ajax to request an ASPX backend method, but be aware of the following points:

1, first to the top of the method with [Wenmethod] characteristics (this attribute to introduce using System.Web.Services; namespaces)

2, the second method must be static (statically) method.

Here are some examples of different methods of invoking a background:

1. Return string method without parameters

Front Code:

1  //returns a string method without parameters2 $.ajax ({3Type: "POST",4URL: "Returns a JSON array in the background." Aspx/hello,5ContentType: "Application/json;charset=utf-8",6Successfunction(msg) {7 alert (MSG.D);8                 }9})
View Code

Background code:

1  /// <summary>2     ///return string method without parameters3     /// </summary>4     /// <returns></returns>5 [WebMethod]6      Public Static  stringHello ()7     {8         return "Hello world!";9}
View Code

2. Return string method with parameter

Front Code:

1 //returns a string method with a parameter2 $.ajax ({3Type"Post",4Url:"returns a JSON array in the background. Aspx/sayhi",5Data"{\ "name\": \ "xiaoming \", \ "age\": \ "18\"}",6ContentType:"Application/json;charset=utf-8",7 success:function (msg) {8 alert (MSG.D);9                 }Ten})
View Code

Background code:

1 /// <summary>2     ///returns a string method with a parameter3     /// </summary>4     /// <param name= "name" ></param>5     /// <param name= "age" ></param>6     /// <returns></returns>7 [WebMethod]8      Public Static stringSayhi (stringName,intAge )9     {Ten         return string. Format ("Hello everyone, my name is {0}, I am {1} years old this year! ", name,age); One}
View Code

3. Return to the list collection in the background

Front Code:

1  //returns the list collection in the background2 $.ajax ({3Type"Post",4Url:"returns a JSON array in the background. Aspx/jiazai",5Data"{\ "name\": \ "Xiao Ming \"}",6ContentType:"Application/json; Charset=utf-8",7                 //dataType: "JSON",8 success:function (msg) {9 $ (MSG.D). each (function () {TenAlert This); One                     }) A                 } -})
View Code

Background code:

1 /// <summary>2     ///return collection method with parameters3     /// </summary>4     /// <param name= "name" ></param>5     /// <returns></returns>6 [WebMethod]7      Public Staticlist<string> Jiazai (stringname)8     {9list<string> ls =Newlist<string>();Ten ls. ADD (name); One         returnls; A}
View Code

Finally add a standard notation: The amount of the front desk

1 //the standard notation:2 $.ajax ({3Type"Post",4DataType:"JSON",5ContentType:"Application/json",//Note: WebMethod () must add this, otherwise the client data will not be uploaded to the server6data:{above},//Note: The data parameter can be a string int type7Url:"list.aspx/deletenews",//impersonate a Web service, commit to a method8      //Optional Async:false, blocking async is synchronous9 beforesend:function () {Ten           //Do something. One           //disable buttons, etc. to prevent users from repeating the submission A$("#btnClick"). attr ({disabled:"Disabled"}); -           //or display a loading picture -      }, the success:function (data) { -Alert"Success:"+ DATA.D);//Note here: You must pass DATA.D to get the value returned to the server -           //The server can return the model directly, or it can return the serialized string if it needs to deserialize: string json = Json.parse (DATA.D); -           //sometimes nested calls to Ajax requests are also possible. +      }, - complete:function () { +           //Do something. A$("#btnClick"). Removeattr ("Disabled"); at           //Hide Loading Pictures - }, - error:function (data) { -Alert"Error:"+DATA.D); -      } -});
View Code

JQuery Ajax Request Aspx backend method

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.