Home > Web Front-end > JS Tutorial > body text

jQuery calls RESTful WCF sample code (GET method/POST method)_jquery

WBOY
Release: 2016-05-16 17:02:23
Original
1204 people have browsed it

No more nonsense, let’s get straight to the point

wcf end:

Restful has become more popular in recent years. In order to enable ajax calls and to support restful-style uri, after creating an Ajax-enabled Wcf Service, you must manually modify the svc file and specify the Factory, that is:

<%@ ServiceHost Language="C#" Debug="true" Service="ajaxSample.HelloWorld" CodeBehind="HelloWorld.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Note: If Factory is not added, wcf will not be directly accessible using restful methods like http://localhost/helloWorld.svc/Hello/person/name.

At the same time, remove in web.config, which is similar to:


                                                        🎜>



"
multipleSiteBindingsEnabled="true" /> binding="webHttpBinding" contract="ajaxSample.HelloWorld" />




Okay, let’s start writing code. Since there are two methods of GET/POST when calling wcf, let’s write an example method for several commonly used situations:




Copy the code

The code is as follows:


using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

namespace ajaxSample
{
    [ServiceContract(Namespace = "http://yjmyzz.cnblogs.com/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class HelloWorld
    {

        ///
        /// 只能Post的Restful方法
        ///

        ///
        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "PostRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
        public List PostRestfulTest(string person,string welcome)
        {
            List result = new List();

            result.Add("PostRestfulTest -> from server:");
            result.Add(person);
            result.Add(welcome);
            return result;
        }

        ///
        /// 只能Get的Restful方法
        ///

        ///
        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GETRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
        public List GETRestfulTest(string person, string welcome)
        {
            List result = new List();

            result.Add("GETRestfulTest -> from server:");
            result.Add(person);
            result.Add(welcome);
            return result;
        }

        ///
        /// 即可Get与Post的Restful方法
        ///

        ///
        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "*", UriTemplate = "RestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
        public List RestfulTest(string person, string welcome)
        {
            List result = new List();

            result.Add("RestfulTest -> from server:");
            result.Add(person);
            result.Add(welcome);
            return result;
        }

 
        ///
        /// 只能Post的常规方法(注:Post方式,BodyStyle必须设置成WrappedRequest或Wrapped)
        ///

        ///
        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
        public List PostTest(string person, string welcome)
        {
            List result = new List();

            result.Add("PostRestfulTest -> from server:");
            result.Add(person);
            result.Add(welcome);
            return result;
        }

        ///
        /// 只能Get的常规方法
        ///

        ///
        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        public List GETTest(string person, string welcome)
        {
            List result = new List();

            result.Add("GETTest -> from server:");
            result.Add(person);
            result.Add(welcome);
            return result;
        }

         

         
    }
}

jQuery调用代码:
复制代码 代码如下:


   


   

       
   




示例代码:点击下载 
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template