How to call the WCF Service using ajax (with demo download) _ jQuery

Source: Internet
Author: User
This article mainly introduces jQuery's method for calling the WCF Service through ajax. It analyzes jQuery's ajax front-end call and the techniques related to the background interactive call of the WCF Service in the form of a complete example, A complete example is provided for readers to download. If you need it, you can refer to the following example to describe how jQuery implements ajax to call the WCF Service. We will share this with you for your reference. The details are as follows:

AJAX calls to the WCF Service are divided into two methods: Cross-origin and non-Cross-origin. Today we will first introduce the call methods under non-Cross-origin. The DEMO is written in VS2008.

After tests and research, it is found that AJAX calls to the WCF Service must meet the following conditions:

1. The communication method of wcf must use webHttpBinding.
2. Required Node Value
3. The service implementation must be marked

The Code is as follows:

[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode. Allowed)]


4. Add the following mark before the Method

The Code is as follows:

[WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. Bare, ResponseFormat = WebMessageFormat. Json)]


5. The parameter name passed in the ajax method must be the same as the parameter method name provided in the wcf Service.

The following code is written by myself. You need to pay attention to the color.

Server Configuration File Code

    
      
        
       
         
         
          
                  
          
        
      
     
      
        
          
           
           
           
          
        
      
      
        
        
       
      
   
 

Server code

[ServiceContract] public interface IService1 {[OperationContract] string GetData (int value); [OperationContract] City GetDataUsingDataContract (City composite); [OperationContract] List
 
  
GetList (); [OperationContract] List
  
   
GetListData (List
   
    
List) ;}// use the data conventions described in the following example to add a composite type to a service operation. [DataContract] public class City {int seq = 0; string cityID; string ctiyName; [DataMember] public string CityID {get {return cityID;} set {cityID = value ;}} [DataMember] public string CityName {get {return ctiyName;} set {ctiyName = value;} [DataMember] public int Seq {get {return seq ;} set {seq = value ;}}}
   
  
 

Implementation Code

[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode. allowed)] public class Service1: IService1 {[WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. wrappedRequest, RequestFormat = WebMessageFormat. json, ResponseFormat = WebMessageFormat. json)] public string GetData (int value) {return string. format ("You entered: {0}", value) ;}# region IService1 member [WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. bare, ResponseFormat = WebMessageFormat. json)] public City GetDataUsingDataContract (City composite) {City c = new City (); c. cityID = composite. cityID; c. cityName = composite. cityName; c. seq = composite. seq; return c;} [WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. bare, ResponseFormat = WebMessageFormat. json)] public List
 
  
GetList () {List
  
   
List = new List
   
    
(); City cc = new City (); cc. cityID = "1"; cc. cityName = "Beijing"; cc. seq = 3; list. add (cc); City PC3 = new City (); cc1.CityID = "2"; cc1.CityName = "Shanghai"; cc1.Seq = 4; list. add (PC3); return list;} [WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. bare, ResponseFormat = WebMessageFormat. json)] public List
    
     
GetListData (List
     
      
List) {return list;} # endregion}
     
    
   
  
 

Client call code

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "WcfServiceDemoOne. WebForm1" %>      

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.