Reputation: 24092
I have a GWT application with which I would like to get data from endomondo in JSON format and I can obtain it with following IRL :
http://api.mobile.endomondo.com/mobile/api/workout/list?authToken=XXXXXXXXXX
and I will get data like
{"data":[{"duration_sec":2710,"sport":0,"speed_kmh_avg":11.721935764889876,"device_workout_id":"6588152507813057122","feed_id":155634169,"sport2":0,"id":192960126,"altitude_m_max":227.3,"hydration":0.35771,"altitude_m_min":145.5,"has_playlist":false,"burgers_burned":1.075926,"start_time":"2013-05-21 18:57:04 UTC","calories":581,"speed_kmh_max":26.281,"distance_km":8.824012756347656,"has_points":true},]}
But somehow by calling this with GWT http request builder I cannot do it. What should I change in the code ?
String url = "http://api.mobile.endomondo.com/mobile/api/workout/list?authToken=XXXXXXXXXX";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
Request myrequest = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// Couldn't connect to server (could be timeout, SOP violation, etc.)
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
trainingsJSON = JSONParser.parseStrict(response.getText());
trainingsString = response.getText();
Label l = new Label();
l.setText("200 erros status code JSON DATA from endomondo " + response.getText()) ;
RootPanel.get().add(l);
} else {
// Handle the error. Can get the status text from response.getStatusText()
Label l = new Label();
l.setText("JSON DATA from endomondo 3rror part " + response.getText()) ;
RootPanel.get().add(l);
}
}
});
} catch (RequestException e) {
// Couldn't connect to server
}
EDIT
I have tried adding
builder.setHeader("Access-Control-Allow-Origin", "*");
but with no luck.
Upvotes: 0
Views: 1946
Reputation: 9741
When you make CORS requests with GWT, you dont have to add any extra header to your request. What you must do, is check that the server is supporting CORS requests from your site.
If you are managing the site http://api.mobile.endomondo.com
, you have to change the server code to return the appropriate CORS headers when the client sends an OPTION
request.
In case of java server-side you have an example of how to handle CORS in this page using a Filter
.
Note that CORS only works in modern browsers (EDITED: actually old version of certain browsers support it. See the list of browsers in @Thomas comments below).
Upvotes: 2