views:

1129

answers:

4

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?

+2  A: 

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.

Igor Klimer
A: 

Hi, yep this will fail due to SOP. What HTTP response code do you get? Normally in this case in comes back as 0 instead of 200 (OK). A solutions may be to use a JSONP approach I wrote a bit on JSONP with GWT as part of this article: http://www.bristol-gtug.org/?p=76

Daniel Vaughan
A: 

Hi,

I have a problem with the same code, the Http response code is 0. I don't understand why. Can you explain me ?

david hadjedj
A: 

This might be too late. If you are not accessing local resources using a relative path and such, you are right, it's subjected to SOP (same origin policy). The -no-server flag won't be of much help to resolve this issue. To by-pass this issue, read http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_JSONFeedsFromOtherDomain . An even better solution would be to use com.google.gwt.jsonp.client.JsonpRequestBuilder, (remember to inherit inherits name = 'com.google.gwt.jsonp.Jsonp' \) which is utilize by the gdata api ("better" in the sense that you don't have to write it yourself). Hope this helps. Cheers~