
Java/JAX-RS JsonP Example
JsonP is a mechanism to deliver JSON as a javascript file thus bypassing the Same-Origin constraints. Most of the Internet Applications expose a JsonP API. In the example we create a Json Service using RestEasy which is JAX-RS compliant. And then we wrap the Json service with a JsonP filter which gets activated only if the, "http://.. : .. /...?callback=something", callback request parameter is included in the get request in which case it wraps the Json output and returns JsonP. The Json Service returns an object with Name and Email. The Ajax web page makes the JsonP call and displays the Name and Email on the browser.
Before the web page makes the JsonP Ajax request the page shows as below.

After the web page makes the JsonP Ajax request the page shows as below.

Please hit the Java/RestEasy JsonP service directly using http://localhost:8090/JavaJsonPService/rest/nae/json?callback=myjavascriptmethod to see the JsonP response. Replace the port with the port number of your server instance.
C#/WCF JsonP Example
In the example we create a Json Service using WCF. And then we annotate the Json service method with a JsonP annotation which gets activated only if the, "http://.. : .. /...?callback=something", callback request parameter is included in the get request in which case it wraps the Json output and returns JsonP. The Json Service returns an object with Name and Email. The Ajax web page makes the JsonP call and displays the Name and Email on the browser.
After the web page makes the JsonP Ajax request to the C# JsonP service the page shows as below.
Both the examples above will return a normal Json response if the 'callback' parameter is not passed in the request url.
Please hit the C#/WCF JsonP service directly using http://localhost:3687/CSharpJsonPService/NameAndEmailService.svc/json?callback=myjavascriptmethod to see the JsonP response. Replace the port with the port number of your server instance.
Download the source code for both the Java and C# JsonP examples here
