GWT RequestBuilder - 跨站点请求(GWT RequestBuilder - C

2019-06-25 15:09发布

我试图让使用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());
        }
    }
}

Answer 1:

实际上,我们可以从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


Answer 2:

你已经错过看完教程。

从直接引用教程 :

所述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]
     handler.@com.google.gwt.sample.stockwatcher.client.StockWatcher::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
     window[callback + "done"] = true;
   }

    ...


文章来源: GWT RequestBuilder - Cross Site Requests