开发者

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开发者_StackOverflow中文版 anybody know any working solution? I could not find any starting point on the web.

thanks in advance


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜