GWT:如何使用 JSON 发送 POST 跨域请求

发布于 2024-09-16 17:19:51 字数 297 浏览 4 评论 0原文

正如其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖阳 2024-09-23 17:19:51

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. 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.

给妤﹃绝世温柔 2024-09-23 17:19:51

您不能使用 JSONP 执行 POST - 它所做的只是插入

也许您正在寻找的是 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.

困倦 2024-09-23 17:19:51

我也遇到了这个问题,我必须实施一些古怪的方案才能使其发挥作用。幸运的是,我同时控制服务器和客户端。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文