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