GWT RequestBuilder - 跨站点请求

6
我正在尝试使用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());
        }
    }
}
2个回答

5

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

我一直在寻找这样的解决方案,我不想在GWT Java中使用本地JS。[抱歉jonasr,我真的很喜欢这个标题的想法] - Manikandaraj Srinivasan

3

您错过了完成教程的部分。

来自教程的直接引用:

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获得成功响应,但在Firebug中响应为空白。可能是服务器问题吗? - Mayank Pandya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接