2011-04-23 47 views
1

我有我的REST服務:http://localhost:4242/myrestservice/getobject 返回一些JSON數據GWT - 從回調從REST服務沒有回答

然後,我有我的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應用程序犯規得到答案。

任何想法如何使它工作?

UPDATE

這裏是我的結果與螢火蟲:

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本地主機,但在我的代碼我使用127.0.0.1!

媽....

+0

我看了一下SOP,但由於它們都是在本地主機上使用相同的端口,我認爲它不應該是在我的情況的問題:HTTP:// EN .wikipedia.org/wiki/Same_origin_policy,除非我明白錯誤? – guigui42 2011-04-23 22:58:29

+0

你使用Firebug嗎?您可以檢查您的GWT客戶端是否在網絡面板中撥打電話。您還可以看到您的休息服務返回的答案。 – 2011-04-24 08:12:44

+0

我用螢火蟲日誌更新了我的帖子...答案是空的 – guigui42 2011-04-24 11:28:54

回答

2

你說你想使用異步回調,但你永遠不會在你的代碼中使用new AsyncCallback<string>()。這就是如何使異步調用服務器與我的GWT應用程序:(我不使用JSON但其實並不重要,在這裏我想)

request object = new request(); 
object.getMessageXml("test.php", "GET", null, new AsyncCallback<String>() { 

    @Override 
    public void onFailure(Throwable caught) { 
     //error handling 
    } 

    @Override 
    public void onSuccess(String result) { 
     // do some stuff with the result 
    } 
}); 

這是請求類:

public void getMessageXml(String file, String type, String requestData, final AsyncCallback<String> callback) { 

     RootLayoutPanel.get().addStyleName("loading"); 

     final String url = "test/" + file; 
     RequestBuilder rb; 

     try { 
      if(type == "POST") { 
       rb = new RequestBuilder(RequestBuilder.POST, URL.encode(url)); 
      } 
      else { 
       rb = new RequestBuilder(RequestBuilder.GET, URL.encode(url)); 
      } 

      rb.setHeader("Content-Type", "application/x-javascript; charset:ISO-8859-1"); 
      rb.sendRequest(requestData, new RequestCallback() { 

       @Override 
       public void onResponseReceived(Request request, Response response) { 

        if(Response.SC_OK == response.getStatusCode()) { 

         callback.onSuccess(response.getText()); 
        } 
        else { 
         //error 
        } 
        RootLayoutPanel.get().removeStyleName("loading"); 
       } 

       @Override 
       public void onError(Request request, Throwable exception) { 
        callback.onFailure(exception); 
       } 
      }); 
     } 
     catch(RequestException rex) { 
      callback.onFailure(rex); 
     } 
    } 

[編輯:] 也許你找到的東西here:

+0

我也做了這些改變,但我仍然得到response.getStatusCode()= 0 – guigui42 2011-04-24 11:09:00

+0

奇怪。我自己嘗試一些東西... – 2011-04-24 19:41:07

+0

我已經在我的回答中發佈了一些關於gwt和json的信息。也許你對它有所瞭解? – 2011-04-24 19:47:44