2012-06-20 51 views
6

我正在尝试使用GWT请求生成器进行跨站点请求,但是我无法使其工作。正如你所看到的,这是一个很大的样本GWT项目,我已经通过https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite。但我仍然错过了一些东西。GWT RequestBuilder - 跨站点请求

我在这里发布代码。我错过了什么..?

package com.gwt.reqbuilder.client; 

import com.google.gwt.core.client.EntryPoint; 
import com.google.gwt.http.client.Request; 
import com.google.gwt.http.client.RequestBuilder; 
import com.google.gwt.http.client.RequestCallback; 
import com.google.gwt.http.client.RequestException; 
import com.google.gwt.http.client.Response; 
import com.google.gwt.http.client.URL; 
import com.google.gwt.user.client.Window; 

public class GWTRequestBuilder implements EntryPoint 
{ 
    private static final String JSON_URL = "http://localhost:8000/?q=ABC&callback=callback125"; 
    public void onModuleLoad() 
    { 
     GWTPOSTHTTP(); 
    } 

    public void GWTPOSTHTTP() 
    { 
     String postUrl="http://localhost:8000"; 
     String requestData="q=ABC&callback=callback125"; 
     RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, postUrl); 
     try { 
      builder.sendRequest(requestData.toString(), new RequestCallback() 
      { 
       public void onError(Request request, Throwable e) 
       { 
        Window.alert(e.getMessage()); 
       } 
       public void onResponseReceived(Request request, Response response) 
      { 
        if (200 == response.getStatusCode()) 
        { 
         Window.alert(response.getText()); 
        } else { 
         Window.alert("Received HTTP status code other than 200 : "+ response.getStatusText()); 
        } 
      } 
      }); 
     } catch (RequestException e) { 
      // Couldn't connect to server 
     Window.alert(e.getMessage()); 
     } 
    } 
} 

回答

5

实际上,我们可以从GWT RequestBuilder跨站点请求。如果我们能在servlet响应头

Response.setHeader("Access-Control-Allow-Origin","http://myhttpserver"); 
设置

它的工作很酷,如果有人需要GWT项目和Python Servlet,请让我知道,我可以上传文件。

GWT Client Code : https://github.com/manikandaraj/MLabs/tree/master/GWT/GWTClient 
+0

我正在寻找像这样的解决方案,我不想在GWT Java中使用原生JS。 [对不起,jonasr,我真的很喜欢这个标题的想法] –

3

您已经错过了阅读本教程。从tutorial

直接引用:

的RequestBuilder码由给的getJSON方法的调用替代。所以您不再需要将下面的代码在refreshWatchList方法:

 
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); 

try { 
    Request request = builder.sendRequest(null, new RequestCallback() { 
    public void onError(Request request, Throwable exception) { 
     displayError("Couldn't retrieve JSON"); 
    } 

    public void onResponseReceived(Request request, Response response) { 
     if (200 == response.getStatusCode()) { 
     updateTable(asArrayOfStockData(response.getText())); 
     } else { 
      displayError("Couldn't retrieve JSON (" + response.getStatusText() 
      + ")"); 
     } 
    } 
    }); 
} catch (RequestException e) { 
    displayError("Couldn't retrieve JSON"); 
} 

这是广义上你有什么,而应该由下面的教程几行给出JSNI功能所取代:

 
    /** 
    * Make call to remote server. 
    */ 
    public native static void getJson(int requestId, String url, 
     StockWatcher handler) /*-{ 
    var callback = "callback" + requestId; 

    // [1] Create a script element. 
    var script = document.createElement("script"); 
    script.setAttribute("src", url+callback); 
    script.setAttribute("type", "text/javascript"); 

    // [2] Define the callback function on the window object. 
    window[callback] = function(jsonObj) { 
    // [3] 
    [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj); 
    window[callback + "done"] = true; 
    } 

    ... 
+0

我尝试同样的事情,但它会工作的一些链接,而不是一些其他人有你的想法吗?为什么有些网站不回应我。 GWT得到成功回应,但在萤火虫响应空白。可能是服务器问题的任何事情? –