GWT - Way around Access-Control-Allow-Origin?
I'm making an internet application with GWT, and one of the features that I've been stuck on for a few weeks is getting the users contact data from google data. I've tried things like GWT-GData and they don't seem to play nicely with the current version of GWT, so I tried going the more traditional approach with OAuth and doing an HTTP Get request. I haven't been receiving anything back as a response, and couldn't figure out why, and I happened across my javascript error log and I'm getting:
"Origin [site name here] is not allowed by Access-Control-Allow-Origin"
I've done some reading and I have a decent idea of what's going on, but I don't know how to get around it in GWT. I've found plenty of read-ups on how to get around it with other platforms, but I haven't seen anything for 开发者_StackOverflow中文版GWT. Can anyone offer any wisdom?
Edit: Here is the code I'm using:
public static void doGet(String url, String oauthToken) {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
Request request = builder.sendRequest(oauthToken, new RequestCallback() {
@Override
public void onError(Request request, Throwable e) {
GWT.log(e.toString(), e);
}
@Override
public void onResponseReceived(Request request, Response response) {
Window.alert("HEADER:" + response.getHeadersAsString()
+ "\nSTATUS: " + response.getStatusText()
+ "\nTEXT: " + response.getText());
}
});
} catch (RequestException e) {
GWT.log(e.toString(), e);
}
}
There's nothing you can do but configure the server to accept the origin of the request (i.e. add it to the returned Access-Control-Allow-Origin
.
Because it's GData, it might however simply be a mistake on your side re. the requested URL: there's no Access-Control-Allow-Origin
when you request data in Atom format, only when requesting JSON (and the value is then *
which allows everyone, so shouldn't cause any issue like you're seeing): http://code.google.com/p/chromium/issues/detail?id=45980#c2
While this doesn't answer the original question, the following may help others who have the same underlying issue that arrived at this page (I'm using a GWT client with a Groovy web server). This did the trick on the server:
HttpExchange.getResponseHeaders().add("Access-Control-Allow-Origin","*");
精彩评论