Share via

JSON with Padding (AJAX)

Download sample

This extensibility sample shows how to support JSON with Padding (JSONP) in Windows Communication Foundation (WCF) services. JSONP is a convention used to invoke cross-domain scripts by generating script tags in the current document. The result is returned in a specified callback function. JSONP is based on the idea that tags such as <script src=”http://...” > can evaluate scripts from any domain, and the script retrieved by those tags is evaluated within a scope in which other functions may already be defined.

This sample includes the following components:

The JsonPEncoder is a wrapping encoder on the WCF JSON encoder. It delegates most calls to the wrapped encoder. The WriteMesage methods have been overriden to pad the outgoing message with the callback method. The JsonBehavior is used on the service operation to enable JSONP encoding for that operation. The query parameter of the URI that holds the callback function name is specified as the CallBack property of the behavior.

The client page dynamically adds a script block to the web page. This script block calls the JSONP service and displays the returned customer name and address.

To set up, build and run the sample

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

© 2007 Microsoft Corporation. All rights reserved.