Home Web Front-end JS Tutorial jQuery calls RESTful WCF sample code (GET method/POST method)_jquery

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

May 16, 2016 pm 05:02 PM
restful

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调用代码:
复制代码 代码如下:


   


   

       
   




示例代码:点击下载 
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Flask-RESTful and Swagger: Best practices for building RESTful APIs in Python web applications (Part 2) Flask-RESTful and Swagger: Best practices for building RESTful APIs in Python web applications (Part 2) Jun 17, 2023 am 10:39 AM

Flask-RESTful and Swagger: Best Practices for Building RESTful APIs in Python Web Applications (Part 2) In the previous article, we explored the best practices for building RESTful APIs using Flask-RESTful and Swagger. We introduced the basics of the Flask-RESTful framework and showed how to use Swagger to build documentation for a RESTful API. Book

RESTful API development with Laravel: Building modern web services RESTful API development with Laravel: Building modern web services Aug 13, 2023 pm 01:00 PM

RESTful API Development with Laravel: Building Modern Web Services With the rapid development of the Internet, the demand for Web services is increasing day by day. As a modern Web service architecture, RESTfulAPI is lightweight, flexible, and easy to expand, so it has been widely used in Web development. In this article, we will introduce how to use the Laravel framework to build a modern RESTful API. Laravel is a PHP language

Building a RESTful API using Django Building a RESTful API using Django Jun 17, 2023 pm 09:29 PM

Django is a web framework that makes it easy to build RESTful APIs. RESTfulAPI is a web-based architecture that can be accessed through HTTP protocol. In this article, we will introduce how to use Django to build RESTful APIs, including how to use the DjangoREST framework to simplify the development process. Install Django First, we need to install Django locally. You can use pip to install Django, specifically

How to use Python Flask RESTful How to use Python Flask RESTful Apr 29, 2023 pm 07:49 PM

1. Overview of RESTful REST (RepresentationalStateTransfer) style is a resource-oriented Web application design style that follows some design principles to make Web applications have good readability, scalability, and maintainability. Let's explain each aspect of the RESTful style in detail: Resource identifier: In the RESTful style, each resource has a unique identifier, usually a URL (UniformResourceLocator). URLs are used to identify the location of resources so that clients can access them using the HTTP protocol. For example, a simple URL could be: http

Steps to build a RESTful API using PHP Steps to build a RESTful API using PHP Jun 17, 2023 pm 01:01 PM

With the development and popularity of the Internet, web applications and mobile applications are becoming more and more common. These applications need to communicate with the backend server and get or submit data. In the past, the conventional way to communicate was to use SOAP (Simple Object Access Protocol) or XML-RPC (XML Remote Procedure Call). Over time, however, these protocols were deemed too cumbersome and complex. Modern applications require more lightweight and straightforward APIs to communicate. RESTfulAPI (presentation layer state conversion AP

Best practices for developing RESTful services with Beego Best practices for developing RESTful services with Beego Jun 23, 2023 am 11:04 AM

In the current environment of continuous innovation in information technology, RESTful architecture is popular in various commonly used WebAPI applications and has become an emerging service development trend. The Beego framework, as a high-performance and easily scalable Web framework in Golang, is widely used in the development of RESTful services due to its advantages of efficiency, ease of use, and flexibility. The following will provide some reference for developers from the perspective of best practices for developing RESTful services in Beego. 1. Routing design in REST

RESTful API design and implementation method RESTful API design and implementation method Jun 22, 2023 pm 04:07 PM

RESTfulAPI is a commonly used API design style in the current Web architecture. Its design concept is based on the standard method of the HTTP protocol to complete the representation and interaction of Web resources. During the implementation process, RESTful API follows a series of rules and constraints, including cacheability, server-client separation, statelessness, etc. These rules ensure the maintainability, scalability, security and ease of use of the API. Next, this article will introduce in detail the design and implementation of RESTfulAPI

How to develop a RESTful-based API using Java How to develop a RESTful-based API using Java Sep 21, 2023 pm 03:53 PM

How to use Java to develop a RESTful-based API. RESTful is an architectural style based on the HTTP protocol. It uses the GET, POST, PUT, DELETE and other methods of the HTTP protocol to operate resources. In Java development, some frameworks can be used to simplify the development process of RESTful API, such as SpringMVC, Jersey, etc. This article will introduce you in detail how to use Java to develop a RESTful-based

See all articles