从 GWT 调用远程 Servlet
我试图从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您尝试向自己的服务器和端口(与您的 GWT 页面所在的服务器和端口相同)发出请求,请将第一行替换为:
如果您尝试与不同的服务器或不同的端口通信您自己的服务器,同源策略将阻止您这样做。您需要从您自己的服务器代理它。
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:
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.
远程 Servlet 需要设置您拥有的 CORS 标头:
或者您可以指定如果您不希望其他域与远程 servlet 交互,则只需使用您自己的域,而不是
*
。The remote servlet is the one that needs to set the CORS header that you have:
Alternatively you can specify just your own domain instead of
*
if you don't want other domains interacting with the remote servlet.我添加了:
到.gwt.xml
然后用
JsonpRequestBuilder 替换
(在服务器和客户端之间转换GWT-PRC
JSONP
)I've added :
<add-linker name="xs" />
to.gwt.xml
And then replaces
GWT-PRC
byJsonpRequestBuilder
(transformingJSONP
between the server and the client)