ajax http post,AJAX Service Using HTTP POST

AJAX Service Using HTTP POST

03/30/2017

2 minutes to read

In this article

The PostAjaxService sample demonstrates how to use Windows Communication Foundation (WCF) to create an ASP.NET Asynchronous JavaScript and XML (AJAX) service that uses HTTP POST. An AJAX service is one that you can access by using basic JavaScript code from a Web browser client. This sample builds on the Basic AJAX Service sample; the only difference between the two samples is the use of HTTP POST instead of HTTP GET.

AJAX support in Windows Communication Foundation (WCF) is optimized for use with ASP.NET AJAX through the ScriptManager control. For an example of using WCF with ASP.NET AJAX, see the Ajax Samples.

Note

The setup procedure and build instructions for this sample are located at the end of this topic.

The service in the following sample is a WCF service with no AJAX-specific code.

If the WebInvokeAttribute attribute is applied on an operation, or the WebGetAttribute attribute is not applied, the default HTTP verb ("POST") is used. POST requests are harder to construct than GET requests, but they are not cached; use POST requests for all operations where caching is not appropriate.

[ServiceContract(Namespace = "PostAjaxService")]

public interface ICalculator

{

[WebInvoke]

double Add(double n1, double n2);

//Other operations omitted…

}

Create an AJAX endpoint on the service by using the WebScriptServiceHostFactory, just as in the Basic AJAX Service sample.

Unlike GET requests, you cannot invoke POST services from the browser. For example, navigating to http://localhost/ServiceModelSamples/service.svc/Add?n1=100&n2=200 results in an error, because the POST service expects the n1 and n2 parameters to be sent in the message body in the JSON format, and not in the URL.

The client Web page PostAjaxClientPage.aspx contains ASP.NET code to invoke the service whenever the user clicks one of the operation buttons on the page. The service responds in the same way as in the Basic AJAX Service sample, with the GET request.

To set up, build, and run the sample

Build the solution PostAjaxService.sln as described in Building the Windows Communication Foundation Samples.

Navigate to http://localhost/ServiceModelSamples/PostAjaxClientPage.aspx (do not open PostAjaxClientPage.aspx in the browser from the project directory).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值