2012-02-02 90 views
0

我是GWT初學者。我在GWT開發模式下調試我的程序。網址是http://127.0.0.1:8888/Replayer.html?gwt.codesvr=127.0.0.1:9997如何在GWT開發模式下獲取遠程服務器的數據?

我想從現有服務器獲取數據,它以json格式提供數據。我的代碼是:

String url = "http://i.abc.com?sid=" + mSessionId + "&action=info"; 
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url)); 

    try { 
     Request request = builder.sendRequest(null, new RequestCallback() { 
      public void onError(Request request, Throwable exception) { 
       // Couldn't connect to server (could be timeout, SOP 
       // violation, etc.) 
       Window.alert("Get fudao info error"); 
       mPrepare = false; 
      } 

      @Override 
      public void onResponseReceived(Request request, Response response) { 
       GWT.log("statuscode:"+response.getStatusCode()); 
       if (200 == response.getStatusCode()) { 
        // Process the response in response.getText() 
        Window.alert(response.getText()); 
        mPrepare = true; 
       } else { 
        // Handle the error. Can get the status text from 
        // response.getStatusText() 
        Window.alert("Get fudao info wrong"); 
        mPrepare = false; 
       } 
      } 
     }); 
    } catch (RequestException e) { 
     // Couldn't connect to server 
    } 

當運行應用程序時,請求失敗,其狀態被「取消」。是否我不能從本地主機請求SOP限制的遠程服務器地址?

如何在GWT開發模式下獲取遠程服務器的數據?

+0

是否只想在開發模式下從遠程服務器獲取數據,還是想在生產環境中執行此操作? – Peter 2012-02-02 12:18:10

+1

您是否聽說過「同源」政策?如果沒有,請閱讀:http://en.wikipedia.org/wiki/Same_origin_policy – Renato 2012-02-02 19:19:33

回答

0

通常無法從其他服務器獲取來自GWT客戶端代碼的數據。但是您的本地服務器可以充當代理,例如你向本地服務器發送請求,它將向遠程服務器發送請求,而不是從遠程服務器獲得響應並將其提供給GWT客戶端代碼。這基本上是最簡單的解決方案。

相關問題