AJAX Service Using HTTP POST

This 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.


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.


The samples may already be installed on your computer. Check for the following (default) directory before continuing.


If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.


To set up, build, and run the sample

  1. Ensure that you perform the setup instructions One-Time Setup Procedure for the Windows Communication Foundation Samples.

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

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

See Also