java GWT RequestBuilder - 跨站点请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11121374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:53:29  来源:igfitidea点击:

GWT RequestBuilder - Cross Site Requests

javagwthttprequest

提问by Manikandaraj Srinivasan

I'm trying to make Cross Site Request using GWT Request builder, which i couldn't get it to work yet. As you can see, this is much of a Sample GWT Project and i have gone through https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite. But still i'm missing something.

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

I'm Posting the code here. What am i missing ..?

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

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

采纳答案by Manikandaraj Srinivasan

Actually we can make Cross Site Requests from GWT RequestBuilder if we can set in Servlet Response Header

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

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

It's working Cool , if anyone need the GWT Project and Python Servlet, please do let me know, i can upload the files.

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

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

回答by jonasr

You've missed to finish reading the tutorial.

您错过了阅读本教程的机会。

Direct quote from the tutorial:

直接引用教程

The RequestBuilder code is replaced by a call to the getJson method. So you no longer need the following codein the refreshWatchList method:

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

Which is broadly what you've got, and should be replaced by a JSNI function given in the tutorial a few lines below :

这大致上是你所拥有的,应该由教程中给出的 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;
   }

    ...