2012-06-20 34 views
6

Tôi đang cố gắng thực hiện Yêu cầu trang web chéo bằng cách sử dụng Trình xây dựng yêu cầu GWT mà tôi chưa thể làm việc đó. Như bạn có thể thấy, đây là một phần của Dự án mẫu GWT và tôi đã trải qua https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite. Nhưng tôi vẫn còn thiếu cái gì đó.GWT RequestBuilder - Yêu cầu trang web chéo

Tôi đang đăng mã ở đây. Tôi đang thiếu gì ..?

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

Trả lời

5

trên thực tế chúng ta có thể thực hiện yêu cầu Site Chữ thập từ GWT RequestBuilder nếu chúng ta có thể thiết lập trong Servlet đáp ứng Tiêu đề

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

Nó hoạt động hiệu quả, nếu có ai cần Dự án GWT và Python Servlet, vui lòng cho tôi biết, tôi có thể tải tệp lên.

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

Tôi đang tìm kiếm một giải pháp như thế này, tôi không muốn sử dụng JS nguyên gốc trong GWT Java. [Xin lỗi jonasr, tôi thực sự thích ý tưởng tiêu đề này] –

3

Bạn đã quên đọc xong hướng dẫn.

quote trực tiếp từ tutorial:

Mã RequestBuilder được thay thế bằng một cuộc gọi đến các phương pháp getJSON. Vì vậy, bạn không còn cần đoạn mã sau trong phương pháp 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"); 
} 

Đó là một cách rộng rãi những gì bạn đã có, và cần được thay thế bằng một hàm JSNI đưa ra trong hướng dẫn một vài dòng dưới đây:

 
    /** 
    * 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; 
    } 

    ... 
+0

Tôi thử điều tương tự nhưng nó sẽ hoạt động đối với một số liên kết chứ không phải cho một số người khác có bất kỳ ý tưởng nào không? tại sao một số url không trả lời tôi. GWT nhận được phản hồi thành công nhưng trong phản hồi firebug là trống. Có thể là bất kỳ điều gì với vấn đề máy chủ ?. –

Các vấn đề liên quan