GWT: How to send POST cross domain requests with JSON

梦想与她 提交于 2019-12-10 00:08:30

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3601804/gwt-how-to-send-post-cross-domain-requests-with-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!