开发者

Calling remote Servlet from GWT

I am trying to call remote servlet from GWT, actually the GWT-RPC doesn't seems to work, so I am trying to do it using the RequestBuilder.

Here's the code snippet:

    String url = "http://some-remote-host:8888/GWTJSTest/SomeServlet?name=" + textBox.getText();
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
//              requestBuilder.setHeader("Origin", "*");
//              requestBuilder.setHeader("Access-Control-Allow-Origin", "*");
    try
    {
        requestBuilder.sendRequest(null, new RequestCallback()
        {
            public void onResponseReceived(Request request, Response response)
            {
                if (response.getStatusCode() == 200)
                {
                    Window.alert(response.getText());
                }else
                {
                    Window.alert(response.getText() + " : " + response.getStatusCode() + response.getStatusText());
                }
            }

            public void onError(Request arg0, Throwable arg1)
            {
                Window.开发者_开发技巧alert(arg1.toString());

            }
        });
    } catch (RequestException e)
    {
        Window.alert("CATCH BLOCK: " + e.getMessage());
        e.printStackTrace();
    }

Actually, IE8 returns the data but after a warning message, but Firefox doesn't! Why is this?

As you see, I am trying to set some request headers but no way.


If you're trying to make a request to your own server and port (the same one that your GWT page is on), replace the first line with:

String url = "/GWTJSTest/SomeServlet?name=" + textBox.getText();

If you're trying to talk to a different server, or a different port on your own server, the Same Origin Policy will keep you from doing that. You'll need to proxy it from your own server.


The remote servlet is the one that needs to set the CORS header that you have:

Access-Control-Allow-Origin: *

Alternatively you can specify just your own domain instead of * if you don't want other domains interacting with the remote servlet.


I've added : <add-linker name="xs" /> to .gwt.xml

And then replaces GWT-PRC by JsonpRequestBuilder (transforming JSONP between the server and the client)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜