Reputation: 2500
I have my REST service : http://localhost:4242/myrestservice/getobject which returns some JSON data
Then, i have my GWT client : http://localhost:4242/gwtclient that client is supposed to do Async calls to the REST service.
Im pretty new to this, so my first idea was to do the following :
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode("http://localhost:4242/myrestservice/getobject"));
builder.setHeader("Accept", "application/json");
builder.setCallback(new MyObjectDescCallback());
builder.setRequestData("");
Request req = builder.send();
...
final class MyObjectDescCallback implements RequestCallback {
public void onError(Request request, Throwable exception) {
showAlert("error = "+exception.getMessage());
GWT.log(exception.getMessage());
}
public void onResponseReceived(Request request, Response response) {
showAlert("response = " + response.getStatusCode());
//Do my stuff here
}
}
}
Unfortunately,i get no response.
I checked on the Rest service, and it does send the object
It seems that my GWT app doesnt receive the answer.
Any idea how to make it work ?
UPDATE
Here are my results with Firebug :
Response Headers
Server Apache-Coyote/1.1
Content-Type application/json
Transfer-Encoding chunked
Date Sun, 24 Apr 2011 11:04:15 GMT
Request Headers
Host 127.0.0.1:4242
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0
Accept application/json
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Referer http://localhost:4242/gwtclient/gwtclient.html
Origin http://localhost:4242
But i get response.getStatusCode() = 0 and empty response !
UPDATE 2 :
I feel so dumb now...
The issue was in fact SOP ...
I was using the URL localhost, but in my code i was using 127.0.0.1 !
Damn....
Upvotes: 1
Views: 2740
Reputation: 950
You say you want to use a Async callback but you never use new AsyncCallback<string>()
in your code. This is how I make async calls to a server with my gwt application: (I don't use JSON but that doesn't really matter here I think)
request object = new request();
object.getMessageXml("test.php", "GET", null, new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
//error handling
}
@Override
public void onSuccess(String result) {
// do some stuff with the result
}
});
And this is the request class:
public void getMessageXml(String file, String type, String requestData, final AsyncCallback<String> callback) {
RootLayoutPanel.get().addStyleName("loading");
final String url = "test/" + file;
RequestBuilder rb;
try {
if(type == "POST") {
rb = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
}
else {
rb = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
}
rb.setHeader("Content-Type", "application/x-javascript; charset:ISO-8859-1");
rb.sendRequest(requestData, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
if(Response.SC_OK == response.getStatusCode()) {
callback.onSuccess(response.getText());
}
else {
//error
}
RootLayoutPanel.get().removeStyleName("loading");
}
@Override
public void onError(Request request, Throwable exception) {
callback.onFailure(exception);
}
});
}
catch(RequestException rex) {
callback.onFailure(rex);
}
}
[EDIT:] Maybe you find something here:
Upvotes: 2