GWT does not support synchronous Ajax, so you have to code your app using asynchronous pattern.
The low level object that GWT uses to perform the request is a XMLHttpRequest (except for old IE versions), and GWT always calls it's open()
method with async set to true. So the only way to have synchronous ajax is maintaining your own modified version of XMLHttpRequest.java
. But synchronous ajax is a bad idea, and even jQuery has deprecated this possibility.
So the normal way in gwt should be that your method returns void
, and you passes an additional parameter with a callback to execute when the response is available.
public void getFolderJson(String path, Callback<String, String> callback) {
RequestBuilder builder = new RequestBuilder(...);
try {
builder.sendRequest(null, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
callback.onSuccess(response.getText());
}
@Override
public void onError(Request request, Throwable exception) {}
callback.onFailure(exception.getMessage());
});
} catch (RequestException e) {
callback.onFailure(exception.getMessage());
}
}
I'd rather gwtquery Promises
syntax for this instead of request-builder one:
Ajax.get("http://localhost/folder?sessionId=foo&path=bar")
.done(new Function(){
public void f() {
String text = arguments(0);
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…