Jquery calls Web Service

Source: Internet
Author: User

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 

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.