0
votes

I'm a GWT beginner. I debug my program in GWT development mode. The url is http://127.0.0.1:8888/Replayer.html?gwt.codesvr=127.0.0.1:9997.

I want to get data from existing server which provided data in json format. My code is:

String url = "http://i.abc.com?sid=" + mSessionId + "&action=info";
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));

    try {
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                // Couldn't connect to server (could be timeout, SOP
                // violation, etc.)
                Window.alert("Get fudao info error");
                mPrepare = false;
            }

            @Override
            public void onResponseReceived(Request request, Response response) {
                GWT.log("statuscode:"+response.getStatusCode());
                if (200 == response.getStatusCode()) {
                    // Process the response in response.getText()
                    Window.alert(response.getText());
                    mPrepare = true;
                } else {
                    // Handle the error. Can get the status text from
                    // response.getStatusText()
                    Window.alert("Get fudao info wrong");
                    mPrepare = false;
                }
            }
        });
    } catch (RequestException e) {
        // Couldn't connect to server
    }

When run the application, the request failed and its status was "canceled". Is it the reason that I cannot request remote server address from localhost for SOP restrictions?

How to fetch data of remote server in GWT development mode?

1
Do you want to fetch data from a remote server only in development mode or do you want to do that in production as well? - Peter
Have you heard of the "same-origin" policy? If not, read this: en.wikipedia.org/wiki/Same_origin_policy - Renato

1 Answers

0
votes

Normally can't fetch data from another server form GWT client code. But your local server can serve as proxy, e.g. you sending request to your local server, it will send request to remote server, than it will get response from remote server and give it to the GWT client code. This is basically the easiest solution.