Home  >  Article  >  Web Front-end  >  Introduction to how jQuery implements ajax to call WCF services

Introduction to how jQuery implements ajax to call WCF services

不言
不言Original
2018-07-02 14:03:041629browse

This article mainly introduces jQuery's method of implementing ajax to call WCF services. It analyzes the related techniques of jQuery's ajax front-end calling and background interactive calling of WCF services in the form of a complete example. It also comes with a complete example for readers to download. Friends who need it You can refer to the following

The example of this article describes how jQuery implements ajax to call WCF services. I share it with you for your reference. The details are as follows:

Regarding AJAX calling WCF services, there are two methods: cross-domain and non-cross-domain. Today we will first introduce the non-cross-domain calling method. DEMO was written in VS2008.

After testing and research, it was found that AJAX must meet the following conditions when calling WCF services

1. The communication method of wcf must use webHttpBinding
2. Must be set< ;endpointBehaviors>The value of the node
3. The implementation of the service must add the mark

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

4. The following mark must be added in front of the method

[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 and method names provided in the wcf service are consistent

The following is the code written by me, the colors marked are the places that need attention

Server-side configuration file code

 
   
    
     
   
     
     
      
       
      
     
    
   
   
    
     
      
      
      
      
     
    
   
   
    
   
   
   

Server-side code

[ServiceContract] 
 public interface IService1 
 { 
  [OperationContract] 
  string GetData(int value); 
  [OperationContract] 
  City GetDataUsingDataContract(City composite); 
   [OperationContract] 
  List GetList(); 
   [OperationContract] 
  List GetListData(List list); 
 } 
 // 使用下面示例中说明的数据约定将复合类型添加到服务操作。 
 [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 成员 
  [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="北京"; 
   cc.Seq = 3; 
   list.Add(cc); 
   City cc1 = new City(); 
   cc1.CityID = "2"; 
   cc1.CityName = "上海"; 
   cc1.Seq = 4; 
   list.Add(cc1); 
   return list; 
  } 
  [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] 
  public List GetListData(List list) 
  { 
   return list; 
  } 
  #endregion 
}

Client calling code

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



The above is the entire content of this article. I hope it will be helpful to everyone’s learning. For more related content, please pay attention to PHP Chinese net!

Related recommendations:

Implementation of jQuery multi-level accordion menu

jquery$.ajax() method

The above is the detailed content of Introduction to how jQuery implements ajax to call WCF services. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn