You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Tharindu <th...@gmail.com> on 2013/04/21 06:22:12 UTC

Re: Struts2 REST Plugin - JSONP support

*Enabling JSONP Support for REST plugin*

JSONP or "JSON with padding" is a communication technique used in
JavaScript programs which run in Web browsers. It provides a method to
request data from a server in a different domain, something prohibited by
typical web browsers because of the same origin policy.

Under the same origin policy, a web page served from
server1.example.comcannot normally connect to or communicate with a
server other than
server1.example.com. A few exceptions include the HTML <script> element.
Exploiting the open policy for <script> elements, some pages use them to
retrieve JavaScript code that operates on dynamically generated
JSON-formatted data from other origins. This usage pattern is known as
JSONP. Requests for JSONP retrieve not JSON, but arbitrary JavaScript code.
They are evaluated by the JavaScript interpreter, not parsed by a JSON
parser.

References- http://en.wikipedia.org/wiki/JSONP

//JSON
{"name":"struts","id":2.3.9}
//JSONP
func({"name":"struts","id":2.3.9});


I deployed "struts2-rest-showcase" sample application
(struts-2.3.8-all\struts-2.3.8\apps\struts2-rest-showcase.war) to test this
functionality.

*JSON Request*
http://localhost:8080/rest/orders.json
Response:
[{"amount":33,"clientName":"Bob","id":"3"},{"amount":66,"clientName":"Jim","id":"5"},{"amount":44,"clientName":"Sarah","id":"4"}]
Type: application/json


*JSONP Request*
http://localhost:8080/rest/orders.json?callback=parseResponse
Response:
[{"amount":33,"clientName":"Bob","id":"3"},{"amount":66,"clientName":"Jim","id":"5"},{"amount":44,"clientName":"Sarah","id":"4"}]
Type: application/json

The output should be:
parseResponse<http://localhost:8080/rest/orders.json?callback=parseResponse>
(
[{"amount":33,"clientName":"Bob","id":"3"},{"amount":66,"clientName":"Jim","id":"5"},{"amount":44,"clientName":"Sarah","id":"4"}]
)

Thanks & regards,
Tharindu Rajarathna



> 2013/2/7 Tharindu <th...@gmail.com>:
> > I discovered that the restful web services developed using the REST
> plugin
> > are not responded according to jsonp (callbacks) requests.Is there any
> way
> > to fix that?
> As I am not a JSONP expert, could you prepare a small demo to show
> what is wrong?
> Thanks in advance & regards
> --
> Ɓukasz
> + 48 606 323 122 http://www.lenart.org.pl/