I recently studied Jquery and Web Serivce. Can I use them together? As a result, we began to explore and found some problems during the exploration process. We all know that the data format returned by both Web Service and WCF is XML, XML file format is large, complex, and occupies broadband transmission, and it takes resources and time for the server and client to parse XML. Json data format is simple, bandwidth is small, and stable; so next we will use the JSON format.
1. Create a Web application project and create a Web service named WebService. asmx.
2. Write the WebService. asmx method. Below is a simple method;
Note: a) the Web Service cannot be reloaded. Make sure that the Web Service can run.
B) There must be [System. Web. Script. Services. ScriptService]. If it is not available, the service will not be called on the front-end page.
1 UsingSystem;
2 UsingSystem. Collections;
3 UsingSystem. ComponentModel;
4 UsingSystem. Data;
5 UsingSystem. Linq;
6 UsingSystem. Web;
7 UsingSystem. Web. Services;
8 UsingSystem. Web. Services. Protocols;
9 UsingSystem. Xml. Linq;
10 UsingSystem. Text;
11 UsingSystem. Collections. Generic;
12
13 NamespaceJqueryandWCF
14 {
15 /// <Summary>
16 ///Summary of WebService
17 /// </Summary>
18 [WebService (Namespace= "Http://tempuri.org/")]
19 [WebServiceBinding (ConformsTo=WsiProfiles. BasicProfile1_1)]
20 [ToolboxItem (False)]
21 //To allow ASP. net ajax to call this Web service from a script, uncomment the downstream service.
22 [System. Web. Script. Services. ScriptService]
23 Public ClassWebService: System. Web. Services. WebService
24 {
25 [WebMethod]
26 Public StringHelloWorld (StringUserName)
27 {
28 Return "Hello" +UserName+"!";
29 }
30 }
31 }
3. On the HTML page, you can click a button to call the service.
Note: 1) data: "{}" in ajax is a parameter used to pass a method. The format is data: "{paraName: paraValue}". If this method has no parameter, the format is: data :"{}"
2) if it succeeds, I will display its value in HTML format. You can use other methods to obtain its value (result. d)
1