【发布时间】:2026-01-19 20:40:01
【问题描述】:
我有我的 REST 服务:http://localhost:4242/myrestservice/getobject 它返回一些 JSON 数据
然后,我有我的 GWT 客户端: http://localhost:4242/gwtclient 该客户端应该对 REST 服务进行异步调用。
我对此很陌生,所以我的第一个想法是执行以下操作:
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode("http://localhost:4242/myrestservice/getobject"));
builder.setHeader("Accept", "application/json");
builder.setCallback(new MyObjectDescCallback());
builder.setRequestData("");
Request req = builder.send();
...
final class MyObjectDescCallback implements RequestCallback {
public void onError(Request request, Throwable exception) {
showAlert("error = "+exception.getMessage());
GWT.log(exception.getMessage());
}
public void onResponseReceived(Request request, Response response) {
showAlert("response = " + response.getStatusCode());
//Do my stuff here
}
}
}
很遗憾,我没有得到任何回应。
我检查了 Rest 服务,它确实发送了对象
我的 GWT 应用似乎没有收到答案。
知道如何让它工作吗?
更新
这是我使用 Firebug 的结果:
Response Headers
Server Apache-Coyote/1.1
Content-Type application/json
Transfer-Encoding chunked
Date Sun, 24 Apr 2011 11:04:15 GMT
Request Headers
Host 127.0.0.1:4242
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0
Accept application/json
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Referer http://localhost:4242/gwtclient/gwtclient.html
Origin http://localhost:4242
但我得到 response.getStatusCode() = 0 和空的回应!
更新 2:
我现在感觉很笨......
问题实际上是 SOP ...
我使用的是 URL localhost,但在我的代码中我使用的是 127.0.0.1 !
该死的……
【问题讨论】:
-
我已经阅读了有关 SOP 的信息,但由于它们都在具有相同端口的 Localhost 上,我相信这对我来说不应该是一个问题:en.wikipedia.org/wiki/Same_origin_policy 除非我理解错误?
-
你使用过 Firebug 吗?您可以在网络面板中检查您的 GWT 客户端是否拨打电话。您还可以看到您的休息服务返回的答案。
-
我用 Firebug 日志更新了我的帖子...答案是空的
标签: json gwt rest asynchronous