Java 中的 JSON 代理 / 玩吧!框架

发布于 2024-12-16 22:19:38 字数 497 浏览 4 评论 0原文

我有一个游戏!应用程序和 JavaScript 我们现在遇到了同源策略问题。

我想要的是 JavaScript ajax 调用转到我们自己的服务器,并且该服务器再次将 json 调用路由到外部 REST API。

我的 JavaScript 使用 ajax 访问此 url:

$.getJSON("http://mydomain.com/users", function(users) {
    //callback          
});

我怎样才能轻松地使服务器路由到让我们说:

public void getUsers(){
     // result = call www.otherdomain.org/api/users.json   What to do here?
     renderJson(result);
}

并返回响应?

或者可以通过直接重新路由来动态完成吗?

I have a Play! application and from the JavaScript we now have run in to the Same Origin Policy Problem.

What I want is that JavaScript ajax calls go to our own server and that this server again route the json call to the external REST API.

My JavaScript use ajax to this url:

$.getJSON("http://mydomain.com/users", function(users) {
    //callback          
});

How can I easly make the server route to lets say:

public void getUsers(){
     // result = call www.otherdomain.org/api/users.json   What to do here?
     renderJson(result);
}

and the return the response?

Or can it be done dynamically somewhere by directly rerouting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小梨窩很甜 2024-12-23 22:19:38

这里有一个执行异步 http 调用的示例(例如 facebook api)

WSRequest req = WS.url("https://graph.facebook.com/100001789213579");
Promise<HttpResponse> respAsync = req.getAsync();
HttpResponse resp = await(respAsync);

JsonElement jsonResp = resp.getJson();
JsonObject jsonObj = new JsonObject();
jsonObj.add("facebook-response", jsonResp);

renderJSON(jsonObj);

here comes an example for doing async http calls (e.g. to facebook api)

WSRequest req = WS.url("https://graph.facebook.com/100001789213579");
Promise<HttpResponse> respAsync = req.getAsync();
HttpResponse resp = await(respAsync);

JsonElement jsonResp = resp.getJson();
JsonObject jsonObj = new JsonObject();
jsonObj.add("facebook-response", jsonResp);

renderJSON(jsonObj);
月牙弯弯 2024-12-23 22:19:38

您可以使用 WS 类 调用另一个URL 作为 Web 服务并检索答案。

请参阅此处的示例

You can use the WS class to call another URL as a web service and retrieve the answer.

See an example here

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