user2395941
user2395941

Reputation: 13

GWT RequestBuilder Post Response return 0 StatusCode

I have created a very simple servlet that uses HTTP Post method. I have tested it on my local Apache Tomcat server using a simple HTML form that works. I want to integrate it with my GWT app. I am able to call it using FormPanel - in that case it downloads content and there is a flicker in my browser window.

I know I need to use RequestBuilder to access it. But my response.getStatusCode() in my overloaded public void onResponseReceived(Request request, Response response) method always return status as 0 and response.getText() return null

    String url = "http://localhost:8080/servlets/servlet/ShapeColor";
    builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));


    try {
        String json = getJSONString();
        //builder.setTimeoutMillis(10000);
        builder.setHeader("Access-Control-Allow-Origin", "*");


        builder.setHeader("Content-type", "application/x-www-form-urlencoded");
        builder.sendRequest(json, new RequestCallback() {

            @Override
            public void onError(Request request, Throwable exception) {
                 Window.alert("Couldn't retrieve JSON");
            }

            @Override
            public void onResponseReceived(Request request, Response response) {
              if (200 == response.getStatusCode()) {
               System.out.println("res:"+response.getText());
              } else {
                  System.out.println("err: " + response.getStatusCode()+","+response.getText());
              }
            }
          });
        //Request response = builder.send();

    } catch (RequestException e) {
        // TODO Auto-generated catch block

    }

I have tried many thing including changing my servlet following CORS reference ( https://code.google.com/p/gwtquery/wiki/Ajax#CORS_%28Cross_Origin_Resource_Sharing%29 )

It always works on browser using my test.html, but not from my App. Although, onResponseReceived method always gets called

Thanks

KKM

Upvotes: 0

Views: 1843

Answers (1)

subrunner
subrunner

Reputation: 404

Have you checked if your call in the app violates the Same-origin policy (http://en.wikipedia.org/wiki/Same-origin_policy) in some way? The GWT RequestBuilder uses XMLHttpRequest internally, so it does fall under the SOP.

Does your GWT app run inside the same domain (server + port) as the servlet? Does it use the same protocol (https or http)?

Upvotes: 1

Related Questions