GWT RequestBuilder – 跨站请求

我正在尝试使用GWT请求构建器创建跨站点请求,我无法使其工作。 正如您所看到的,这是一个样本GWT项目,我已经浏览了https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite 。 但我仍然缺少一些东西。

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

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()); } } } 

实际上,如果我们可以在Servlet响应头中设置,我们可以从GWT RequestBuilder发出跨站请求

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

它很酷,如果有人需要GWT项目和Python Servlet,请告诉我,我可以上传文件。

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

你错过了阅读教程。

直接引用教程 :

RequestBuilder代码被对getJson方法的调用所取代。 因此, 您不再需要 refreshWatchList方法中的以下代码

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

尝试{
  请求请求= builder.sendRequest(null,new RequestCallback(){
     public void onError(请求请求,Throwableexception){
       displayError(“无法检索JSON”);
     }

     public void onResponseReceived(请求请求,响应响应){
       if(200 == response.getStatusCode()){
         updateTable(asArrayOfStockData(response.getText()));
       } else {
           displayError(“无法检索JSON(”+ response.getStatusText()
             +“)”);
       }
     }
   });
 } catch(RequestException e){
   displayError(“无法检索JSON”);
 }

这是你所拥有的,并且应该由教程中给出的JSNI函数替换,如下所示:

   / **
    *拨打远程服务器。
    * /
   public native static void getJson(int requestId,String url,
       StockWatcher处理程序)/ *  -  {
    var callback =“callback”+ requestId;

    // [1]创建一个脚本元素。
    var script = document.createElement(“script”);
    script.setAttribute(“src”,url + callback);
    script.setAttribute(“type”,“text / javascript”);

    // [2]在窗口对象上定义回调函数。
    window [callback] = function(jsonObj){
    // [3]
      。处理程序@ com.google.gwt.sample.stockwatcher.client.StockWatcher :: handleJsonResponse(LCOM /谷歌/ GWT /核心/客户/ JavaScriptObject;)(jsonObj);
      window [callback +“done”] = true;
    }

     ...