Java / Play中的JSON代理!骨架 [英] JSON Proxy in Java / Play! Framework

查看:82
本文介绍了Java / Play中的JSON代理!骨架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩了!应用程序,从JavaScript我们现在已经运行到同源策略问题。

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

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

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.

我的JavaScript使用ajax到此URL:

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);
}

并返回回复?

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

Or can it be done dynamically somewhere by directly rerouting?

推荐答案

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

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);

这篇关于Java / Play中的JSON代理!骨架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆