adranale
adranale

Reputation: 2874

GWT: How to send POST cross domain requests with JSON

As its Javadocs suggest, the JsonpRequestBuilder can only send GET requests. I need to send POST requests using the same method (for cross domain requests using JSON). Does anybody know any working solution? I could not find any starting point on the web.

thanks in advance

Upvotes: 0

Views: 4892

Answers (3)

Lomilar
Lomilar

Reputation: 531

I had this issue as well, and I had to implement a bit of a wacky scheme in order to get it to work. Luckily, I control both the server and client.

The POST call defines a url parameter called 'src' that contains some random string. When I POST to the server, the data goes to the server, but I am not able to get the response.

What happens is behind the scenes the server caches the POST response with that 'src' key in a weak cache.

I then do a JSONP get call immediately after the POST finishes with that same 'src' key, and it fetches the result.

It isn't pretty, but it works.

Upvotes: 0

Jason Hall
Jason Hall

Reputation: 20920

The Google APIs Library for GWT solves this problem (to send cross-domain GWT-RPC calls) by using the Shindig project's gadgets.rpc functionality to send a cross-frame message to an iframe in the page pointing to a page on the server you're trying to communicate with. That iframe is the one that makes the request, and when it receives a response, it sends another cross-frame message back.

This is wrapped up in GadgetsRequestBuilder.

It should be fairly straightforward to extend this functionality to make regular XHR requests (with a POST method) instead of GWT-RPC requests.

Upvotes: 4

Chris Lercher
Chris Lercher

Reputation: 37778

You can't use JSONP to do a POST - all it does is inserting a <script src="..."> tag, and the browser fires off a GET request.

Maybe what you're looking for is CORS, but that's only supported by FF 3.5, IE 8 and Safari 4 and newer. And the server must support it, too.

Otherwise, you'll have to proxy from your server to the other domain.

Upvotes: 1

Related Questions