GWT:如何使用 JSON 发送 POST 跨域请求
正如其 Javadocs 所建议的,JsonpRequestBuilder 只能发送 GET 请求。我需要使用相同的方法发送 POST 请求(对于使用 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Google APIs Library for GWT 解决了这个问题(发送跨域GWT-RPC 调用)通过使用 Shindig 项目的 gadgets.rpc 功能,用于将跨框架消息发送到
iframe
在指向您尝试与之通信的服务器上的页面的页面中。该 iframe 是发出请求的框架,当它收到响应时,它会发回另一条跨框架消息。这包含在
GadgetsRequestBuilder
。扩展此功能以发出常规 XHR 请求(使用 POST 方法)而不是 GWT-RPC 请求应该相当简单。
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. Thatiframe
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.
您不能使用 JSONP 执行 POST - 它所做的只是插入
标记,然后浏览器会触发 GET 请求。
也许您正在寻找的是 CORS ,但仅 FF 3.5、IE 8 和 Safari 4 及更高版本支持。服务器也必须支持它。
否则,您将必须从您的服务器代理到其他域。
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.
我也遇到了这个问题,我必须实施一些古怪的方案才能使其发挥作用。幸运的是,我同时控制服务器和客户端。
POST 调用定义了一个名为“src”的 url 参数,其中包含一些随机字符串。当我 POST 到服务器时,数据会发送到服务器,但我无法获得响应。
在幕后发生的事情是,服务器将带有“src”键的 POST 响应缓存在弱缓存中。
然后,在 POST 使用相同的“src”键完成后,我立即执行 JSONP get 调用,并获取结果。
它不漂亮,但很有效。
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.