Search code examples
javaxmlgwtgethttp-request

GET request using GWT to retrieve XML data?


Oh hello there, fellow SO members,

I have a web service that returns XML data using a simple get request that goes like this :

http://my-service:8082/qc/getData?paramX=0169&paramY=2

the service returns raw xml in the page according to the parameters' values.

I am trying to retrieve this data from a GET request in GWT using RequestBuilder, Request, etc. However, the response gives me empty text, a Status code of ZERO (which doesn't mean anything and isn't supposed to happen), and so on.

Here's the simplified code that doesn't work.

public class SimpleXML implements EntryPoint {

public void onModuleLoad() {

    this.doGet("http://my-service:8082/qc/getData", "0169", "2");
}

public void doGet(String serviceURL, String paramX, String paramY) {
    final String getUrl = serviceURL + "?paramX=" + paramX + "&idTarification=" + paramY;

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, getUrl);

    try {
         Request response = builder.sendRequest(null, new RequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                response.getStatusCode(); // Gives me 0 (zero)  :(
            }

            @Override
            public void onError(Request request, Throwable exception) {
                // ... doesn't matter for this example
            }
        });
    } catch (RequestException e) {
        // ... doesn't matter for this example
    }
}

}

I don't get why this wouldn't work, since this is REALLY simple, I've seen tutorials and they all show me this way of doing things..

Thanks in advance


Solution

  • The reason is, that browsers do not allow cross-site requests with AJAX (see Same Origin Policy).

    This means, that you can only call a service on the same server, same port (using the same protocol) as your HTML page. If you want to perform cross-site requests, you can use JSONP, as explained in http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html.