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
🎜>
" 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
{
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
{
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
{
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
{
List
result.Add("PostRestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
///
/// 只能Get的常规方法
///
///
///
///
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public List
{
List
result.Add("GETTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
}
}
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调用:
示例代码:点击下载

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

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
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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.