GWT Synchronous call

送分小仙女□ 提交于 2019-12-03 21:23:23
AhHatem

You cannot use the native GWT RPC synchronously. I am not sure that this is what you are asking, but here is how to make a call to the server synchronously:

private native String makeSyncAjaxCall(String url, String msgText, String conType)/*-{
    var xhReq = new XMLHttpRequest();
    xhReq.open(conType, url, false);
    if(conType == "POST") xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhReq.send(msgText);
    var serverResponse = xhReq.status + xhReq.responseText;
    return serverResponse;
}-*/; 

Please note that I am not discussing whether that is good idea or not. You should probably stick with Async and put the alert on the success event.

Manikandaraj Srinivasan

GWT doesn't allow to make synchronous calls to the server, as they can make your browser hang until it gets the response, so for obvious reasons it's best if you can change the flow such that the result from the server will be processed inside the onSuccess event handler.

my business need is to make a JS function retrieve data from DB but IT MUST BE SYNCHRONOUS

You can get data from a database only through java (or using any other server-side language), but not using JavaScript. Just make an asynchronous call from GWT using either RequestBuilder or the GWT RPC mechanism. The returned data can than be process inside the appropriate handlers, as mentioned.

Manikandaraj Srinivasan

Ahmad, referring the example you have quoted :

Call a function on Click , say 'getNamefromID()' in GWT , which makes an asynchronous call to the DB and in the onSuccess() function , call a function that will alert the Name.

public void getNamefromID(int ID) {
    String postUrl = "http://mani/getName";
    String requestData = "q=ID";
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
            postUrl);
    builder.setHeader("Access-Control-Allow-Origin", "*");
    try {
        builder.sendRequest(requestData.toString(), new RequestCallback() {
            public void onError(Request request, Throwable e) {
                Window.alert(e.getMessage());
            }

            public void onResponseReceived(Request request,
                    Response response) {
                if (200 == response.getStatusCode()) {
                    Window.alert("Name :" + response.getText());
                } else {
                    Window.alert("Received HTTP status code other than 200 : "
                            + response.getStatusText()
                            + "Status Code :"
                            + response.getStatusCode());
                }
            }
        });
    } catch (RequestException e) {
        // Couldn't connect to server
        Window.alert(e.getMessage());
    }
}
Alberto Guirao

From https://stackoverflow.com/a/40610733/6017801:

GWT calls XMLHttpRequest.open() whith true as its third parameter which means the call will be asynchronous. I solved a similar need for testing purposes just forcing this third parameter to be always false:

private static native void fakeXMLHttpRequestOpen() /*-{
   var proxied = $wnd.XMLHttpRequest.prototype.open;

   (function() {
       $wnd.XMLHttpRequest.prototype.open =
           function() {
                arguments[2] = false;
                return proxied.apply(this, [].slice.call(arguments));
            };
        })();
}-*/;

After invoking fakeXMLHttpRequestOpen(), any further use of XMLHttpRequest will act synchronously. For instance:

remoteSvc.getResult(new AsyncCallback<String>() {
    @Override
    public void onSuccess(String result) {
        GWT.log("Service called!");
    }

    @Override
    public void onFailure(Throwable caught) {
        GWT.log("Service failed...");
    }
}

GWT.log("Last message");

will allways render:

Service called!
Last message

See https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open for XMLHttpRequest.open() specification.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!