search
HomeWeb Front-endJS TutorialjQuery calls RESTful WCF sample code (GET method/POST method)_jquery

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:

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

namespace ashx_jQuery
{                                                                                                                                                                /param>

                                                                      ​if (month == 1 )//Just a demonstration
                                                                                                                               Return 1000;


AjaxProcess.ashx
复制代码 代码如下:

using System.Web;

namespace ashx_jQuery
{
    ///
    /// Summary description for AjaxProcess
    ///

    public class AjaxProcess : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string month = context.Request["month"];

            TestService wcf = new TestService();
            double salary = wcf.GetSalary(GetUserId(), int.Parse(month));
            context.Response.Write("{salary:" salary "}");
        }

 
        ///
        /// 获取当前的用户ID
        ///

        ///
        private int GetUserId() 
        {
            return 1;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

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






    jQuery ashx Sample
   
   


   

       
   




示例代码:点击下载 
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
Flask-RESTful和Swagger: Python web应用程序中构建RESTful API的最佳实践(第二部分)Flask-RESTful和Swagger: Python web应用程序中构建RESTful API的最佳实践(第二部分)Jun 17, 2023 am 10:39 AM

Flask-RESTful和Swagger:Pythonweb应用程序中构建RESTfulAPI的最佳实践(第二部分)在上一篇文章中,我们探讨了如何使用Flask-RESTful和Swagger来构建RESTfulAPI的最佳实践。我们介绍了Flask-RESTful框架的基础知识,并展示了如何使用Swagger来构建RESTfulAPI的文档。本

使用Laravel进行RESTful API开发:构建现代化的Web服务使用Laravel进行RESTful API开发:构建现代化的Web服务Aug 13, 2023 pm 01:00 PM

使用Laravel进行RESTfulAPI开发:构建现代化的Web服务随着互联网的快速发展,Web服务的需求日益增加。而RESTfulAPI作为一种现代化的Web服务架构方式,具备轻量、灵活、易扩展的特点,因此在Web开发中得到了广泛应用。在本文中,我们将介绍如何使用Laravel框架来构建一个现代化的RESTfulAPI。Laravel是PHP语言中

Python Flask RESTful怎么使用Python Flask RESTful怎么使用Apr 29, 2023 pm 07:49 PM

一、RESTful概述REST(RepresentationalStateTransfer)风格是一种面向资源的Web应用程序设计风格,它遵循一些设计原则,使得Web应用程序具有良好的可读性、可扩展性和可维护性。下面我们来详细解释一下RESTful风格的各个方面:资源标识符:在RESTful风格中,每个资源都有一个唯一的标识符,通常是一个URL(UniformResourceLocator)。URL用于标识资源的位置,使得客户端可以使用HTTP协议进行访问。例如,一个简单的URL可以是:http

使用Django构建RESTful API使用Django构建RESTful APIJun 17, 2023 pm 09:29 PM

Django是一个Web框架,可以轻松地构建RESTfulAPI。RESTfulAPI是一种基于Web的架构,可以通过HTTP协议访问。在这篇文章中,我们将介绍如何使用Django来构建RESTfulAPI,包括如何使用DjangoREST框架来简化开发过程。安装Django首先,我们需要在本地安装Django。可以使用pip来安装Django,具体

使用PHP构建RESTful API的步骤使用PHP构建RESTful API的步骤Jun 17, 2023 pm 01:01 PM

随着互联网的发展和普及,Web应用程序和移动应用程序越来越普遍。这些应用程序需要与后端服务器进行通信,并获取或提交数据。在过去,常规的通信方式是使用SOAP(简单对象访问协议)或XML-RPC(XML远程过程调用)。然而,随着时间的推移,这些协议被认为过于笨重和复杂。现代应用程序需要更加轻巧和直接的API来进行通信。RESTfulAPI(表现层状态转化AP

Beego开发RESTful服务的最佳实践Beego开发RESTful服务的最佳实践Jun 23, 2023 am 11:04 AM

在当下信息技术不断创新的环境下,RESTful架构风靡于各种常用的WebAPI应用之中,成为了新兴的服务开发趋势。而Beego框架作为Golang中一款高性能、易扩展的Web框架,出于其高效、易用、灵活等优点,被广泛应用于RESTful服务的开发中。下文将从Beego开发RESTful服务的最佳实践的角度,为广大的开发者提供一些参考。一、路由设计在REST

RESTful API设计及其实现方法RESTful API设计及其实现方法Jun 22, 2023 pm 04:07 PM

RESTfulAPI是目前Web架构中较为常用的一种API设计风格,它的设计理念是基于HTTP协议的标准方法来完成Web资源的表示与交互。在实现过程中,RESTfulAPI遵循一系列规则和约束,包括可缓存、服务器-客户端分离、无状态性等,这些规则保证了API的可维护性、扩展性、安全性以及易用性。接下来,本文将详细介绍RESTfulAPI的设计及其实现方

如何使用Java开发一个基于RESTful的API如何使用Java开发一个基于RESTful的APISep 21, 2023 pm 03:53 PM

如何使用Java开发一个基于RESTful的APIRESTful是一种基于HTTP协议的架构风格,通过使用HTTP协议的GET、POST、PUT、DELETE等方法来实现对资源的操作。在Java开发中,可以使用一些框架来简化RESTfulAPI的开发过程,如SpringMVC、Jersey等。本文将向您详细介绍如何使用Java开发一个基于RESTful的

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.