从 GWT 调用远程 Servlet

发布于 2024-10-16 13:17:09 字数 1289 浏览 8 评论 0原文

我试图从 GWT 调用远程 servlet,实际上 GWT-RPC 似乎不起作用,所以我尝试使用 RequestBuilder 来完成它。

代码片段如下:

    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();
    }

实际上,IE8 在出现警告消息后返回数据,但 Firefox 不会!这是为什么呢?

如您所见,我正在尝试设置一些请求标头,但没有办法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹雨成花 2024-10-23 13:17:09

如果您尝试向自己的服务器和端口(与您的 GWT 页面所在的服务器和端口相同)发出请求,请将第一行替换为:

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

如果您尝试与不同的服务器或不同的端口通信您自己的服务器,同源策略将阻止您这样做。您需要从您自己的服务器代理它。

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.

他夏了夏天 2024-10-23 13:17:09

远程 Servlet 需要设置您拥有的 CORS 标头:

Access-Control-Allow-Origin: *

或者您可以指定如果您不希望其他域与远程 servlet 交互,则只需使用您自己的域,而不是 *

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.

两个我 2024-10-23 13:17:09

我添加了:
.gwt.xml

然后用 JsonpRequestBuilder 替换 GWT-PRC (在服务器和客户端之间转换 JSONP

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文