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

2 に答える 2

5

実際、サーブレットのレスポンス ヘッダーに設定できる場合、GWT RequestBuilder からクロス サイト リクエストを作成できます。

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

クールに動作しています。GWT プロジェクトと Python サーブレットが必要な場合は、お知らせください。ファイルをアップロードできます。

GWT Client Code : https://github.com/manikandaraj/MLabs/tree/master/GWT/GWTClient
于 2012-11-29T21:01:08.337 に答える
3

チュートリアルを読み終えていません。

チュートリアルからの直接引用:

RequestBuilder コードは、getJson メソッドの呼び出しに置き換えられます。そのため、 refreshWatchListメソッドで次のコードは必要なくなりました。

RequestBuilder ビルダー = 新しい RequestBuilder(RequestBuilder.GET, url);

試す {
  リクエスト request = builder.sendRequest(null, new RequestCallback() {
    public void onError(Request request, Throwable exception) {
      displayError("JSON を取得できませんでした");
    }

    public void onResponseReceived(リクエスト リクエスト、レスポンス レスポンス) {
      if (200 == response.getStatusCode()) {
        updateTable(asArrayOfStockData(response.getText()));
      } そうしないと {
          displayError("JSON を取得できませんでした (" + response.getStatusText()
            + ")");
      }
    }
  });
} キャッチ (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.setAttribute("src", url+callback);
   script.setAttribute("type", "text/javascript");

   // [2] ウィンドウ オブジェクトにコールバック関数を定義します。
   ウィンドウ[コールバック] = 関数(jsonObj) {
   // [3]
     handler.@com.google.gwt.sample.stockwatcher.client.StockWatcher::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
     window[コールバック + "完了"] = true;
   }

    ...
于 2012-06-20T14:56:40.957 に答える