I’m using GWT and I want to make a JSONP request, which invokes a GWT method of mine when it returns.
However, I’m having trouble figuring out how to specify the GWT method to invoke on callback. Can anyone help? Here’s my example code:
private native void fetchUserData(String accessToken) /*-{
var callback = "com.company.example.FacebookApi::handleUser";
var url = "https://graph.facebook.com/me?access_token="+accessToken+"&callback=" + callback;
// use jsonp to call the graph
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
}-*/;
public void handleUser(Object o) {
Window.alert("Received object with class: " + o.getClass().getName())
}
This code is ported from this example: Facebook Without SDK.
Alternatively, I just discovered there’s a GWT JsonpRequestBuilder which I haven’t had a chance to use yet, but if anyone can give an example without using any native code… then all the better.
Thanks!
Figured it out, thanks in large part to these examples:
Gwt + JSONP
Cross Domain Requests with Gwt, Jsonp
Cross site referenceing in GWT
Here’s the updated code, per the comments (no callback specified, using Javascript Overlay Type)
}
And the response is automatically wrapped using JSO like so:
For completeness, this is the raw JSON response the JSO wraps. Because of the inheritance, the same
FbUserobject is used if there’s either an error like so:Or the expected User object:
Note the
errorandhometownfields in the JSON response are easily wrapped into JavaScriptObjects.