Search code examples
pythonjsongwtsame-origin-policy

GWT Request Builder problem (same site policy issue?)


I am trying out GWT in this 'configuration':

1) I have written a server backend in python which will produce json output (running at localhot:8094)

2) I have written a very simple GWT app that will use RequestBuilder to set GET to the python server (in development mode of the GWT eclipse plugin, it is accessible via http://127.0.0.1:8888/test.html)

The code is simply

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Test implements EntryPoint {
    /**
     * The message displayed to the user when the server cannot be reached or
     * returns an error.
     */

    private static final String SERVER_URL = "http://localhost:8094";
    private static final String SERVER_ERROR = "An error occurred while "
            + "attempting to contact the server. Please check your network "
            + "connection and try again.";

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {

        RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, SERVER_URL);
        try {
            requestBuilder.sendRequest(null, new Jazz10RequestCallback());
        } catch (RequestException e) {
            Window.alert("Failed to send the message: " 
                    + e.getMessage());
        }

    }

    class Jazz10RequestCallback implements RequestCallback{

        public void onError(Request request, Throwable exception) {
                // never reach here
        Window.alert("Failed to send the message: "
                    + exception.getMessage());

        }

        public void onResponseReceived(Request request, Response response) {
            // render output
            Window.alert(response.getText());

        }


    }
}

However the alert always comes from onResponseReceived and display nothing (empty string I suppose)

I can reach my python server alright and download the json via the browser. But I cannot see any request hitting the server from GWT.

I have ensured that "inherits name='com.google.gwt.http.HTTP" is in the gwt.xml file

Questions are:

1) Is it same site policy restriction at work here? I would expect Exception (and hence the fail message), but it did not happen

2) If it is indeed the same site policy issue, what is the easiest way to deploy the GWT scripts from the python backend? The eclipse gwt plugin produces some artifact in the war subdirectory. Is it sufficient to copy these files to some static directory of my python backend?


Solution

  • 1) Yes it is, while the host is the same, you are trying to access a different port - SOP doesn't allow that. You're probably getting JavaScript exceptions - check Firebug's console or something similar.

    2) Follow the guide in the official docs. You don't need a Java server - just one that can serve HTTP content (so, for example, Apache is fine). I have no experience with Python as the backend, but I'm sure there's a solution for serving Python and HTTP.

    When using the -noserver flag, your external server is used by the GWT Hosted Mode browser to serve up both your dynamic content, and all static content (such as the GWT application's host page, other HTML files, images, CSS, and so on.)

    The dynamic content in this case would be your Python scripts.