Asp.net uses jquery to implement the Ajax callback background Method

Source: Internet
Author: User

To call jquery encapsulated Ajax callback method, then certainly want to introduce jquery's original file into the project, the specific jquery source code can be downloaded to the official website: http://jquery.com/

First, add a button on the foreground:

 
<Div><InputType= "Button"ID= "Mybutton"Value= "Say hello"Onclick= "Ajaxsayhello ()" /></Div>

 

Then we use jquery to implement The onclick event of the button:

    Function  Ajaxsayhello (){  VaR Firstname = "Eric" ;  VaR Lastname = "Sun" ;  VaR Paramobj = New  Object (); paramobj. firstname = Firstname; paramobj. lastname = Lastname;  VaR Paramstr =JSON. stringify (paramobj); $. Ajax ({type: "Post" , URL: "Exportmydata. aspx/sayhello" , Data: paramstr, contenttype: "App/JSON; charset = UTF-8" , Datatype: "JSON" , Success:  Function  (Result) {alert (result. D );  //  Get the data of the result  }, Error:  Function (Result) {alert (result. statustext );}}); 

Here, parameters are transmitted in JSON format,VaRParamstr =JSON. stringify (paramobj); The time object is converted to a JSON string. From the paramobj object, we can see that we need to pass two parameters: firstname and lastname. The actual passed values are Eric and sun. Therefore, the method called in the background must have the firstname and lastname parameters.

 

The background method is as follows:

[Webmethod]Public Static StringSayhello (StringFirstname,StringLastname ){StringSaystr ="Hello"+ Firstname +" "+Lastname;ReturnSaystr ;}

Add the [webmethod] label (Note: Use the following namespace) and set this method to public static ....

 

You need to add additional namespaces:

UsingSystem. Web. Services;

 

More knowledge points ......

Please read this articleArticleHttp://www.cnblogs.com/fish-li/archive/2013/01/13/2858599.html

 

 

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.