Currently, Ajax is very popular, and the backend is the simplest development mode.ProgramOnly Ajax requests are processed, and JSON data is returned. All the previous sections are static html, which uses js to build the interface. Previously, Ajax requests were processed by a general handler, and the results were serialized into JSON and returned to the page using the JSON class library. In this way, backend processing is still quite troublesome, and each request must be either a separate handler or an additional parameter must be added to distinguish different Ajax requests, recently, we found a simpler method that can be called directly using Js. net WebService.CodeNeat, fast development, and easy debugging. Just tap the WebService address in the browser and click a method to call it. The following is a simple example of the construction process:
1. Add a web Empty Project in.
2. Add a web service in the project. My name is "user", and an additional user. asmx file will be added.
3. Add methods and labels that support Ajax script calls to WebService. The Code is as follows:
1 /// <Summary>
2 /// Summary of webservice1
3 /// </Summary>
4 [WebService (namespace = " Http://tempuri.org/ " , Name = " User. asmx " )]
5 [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
6 [System. componentmodel. toolboxitem ( False )]
7 // To allow ASP. Net ajax to call this web service from a script, uncomment the downstream service.
8 [Scriptservice]
9 Public Class Webservice1: system. Web. Services. WebService
10 {
11 [Webmethod]
12 [Scriptmethod (usehttpget =True , Responseformat = responseformat. JSON)]
13 Public People getcurrentuser ()
14 {
15 People P = New People ();
16 P. Name = " Li tuyu " ;
17 P. Age =28 ;
18 P. Detail = " Half-hanging Software Engineer " ;
19 Return P;
20 }
21
22 [Webmethod]
23 [Scriptmethod (usehttpget = True , Responseformat = responseformat. JSON)]
24 Public Int Add ( Int A, Int B)
25 {
26 Return A + B;
27 }
28 }
29
30 Public Class People
31 {
32 Public String Name { Set ; Get ;}
33 Public Int Age { Set ; Get ;}
34 Public String Detail { Set ; Get ;}
35 }
4. Use a static html file for testing. The Code is as follows:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head >
< Title > </ Title >
< Script Type = "Text/JavaScript" SRC = "Script/lib/jquery-1.7.1.js" > </ Script >
< Script Type = "Text/JavaScript" >
Function Callwebservice (URL, Param, Handler ){
$. Ajax ({
URL: URL,
Data: Param,
Type: " Get " ,
Datatype: " JSON " ,
Contenttype: " APP/JSON; charset = UTF-8 " ,
Success: Function (P ){
Handler (P. D );
}
});
};
</ Script >
</ Head >
< Body >
< Script Type = "Text/JavaScript" >
Callwebservice ( " User. asmx/getcurrentuser " , Null , Function (Data ){
Document. Body. innerhtml + = " User name: " + Data. Name + " <Br/> " ;
});
Callwebservice ( " User. asmx/Add " , {: 1 , B: 2 }, Function (Data ){
Document. Body. innerhtml + = " Add result: " + Data;
});
</ Script >
</ Body >
</ Html >