2011-06-29 148 views
0

我需要從運行在不同域上的javascript調用GWT應用程序服務。GWT跨域rpc

這怎麼辦?我如何從我的應用程序指向JavaScript網址?

謝謝。

回答

0

跨域請求背後的想法是你的java腳本創建一個腳本標記,它從僞造url加載生成的java腳本。加載時生成的Java腳本將被評估並調用您創建的回調函數。

下面的代碼IST不testet並給出了這個概念:

public class CrossSiteDomainRequest { 

    /** Counter to create unique ids for callback function. */ 
    private static int idCounter = 0; 

    /** Url to load the javascript from. */ 
    private String url; 

    /** 
    * Creates a new loader with the given <code>url</code>. 
    * @param url to load the java script from {@link #url}. 
    */ 
    public CrossSiteDomainRequest(String url) { 
     this.url = url; 
    } 

    /** 
    * Uses the {@link #url} to load the data from another url. 
    */ 
    public void load() { 
     String callbackId = "callbackId" + idCounter++; 
     String prepend = url.indexOf("?") != -1 ? "&" : "?"; 
     String u = url + prepend + "callback=" + callbackId// Add more Parameters 

     createCallback(this, transId); 

     Element script = DOM.createElement("script"); 
     script.setAttribute("src", u); 
     script.setAttribute("id", callbackId); 
     script.setAttribute("type", "text/javascript"); 
     script.setAttribute("language", "JavaScript"); 

     getHead().appendChild(script); 
    } 

    /** 
    * Destroys the callback with the given <code>id</code>. 
    * @param id of the script tag and native javascript callback function which should be destroyed. 
    */ 
    protected void destroyCallbackmethod(String id) { 
     getHead().removeChild(DOM.getElementById(id)); 
     removeCallback(id); 
    } 

    /** 
    * This method is invoked by the callback to handel the loaded data. 
    * @param callbackId DOM-Id of the callback whick invoked this method. 
    * @param jso Object that encapsultes the loaded data. 
    */ 
    @SuppressWarnings("unchecked") 
    protected void onReceivedData(String callbackId, JavaScriptObject jso) { 
     try { 
      // Read data 
     } catch (Exception e) { 
      // Handle Error 
     } 
     destroyCallbackmethod(callbackId); 
    } 

    /** 
    * Creates a native javascript callback. 
    * @param cscr to invoke the {@link #onReceivedData(String, com.google.gwt.core.client.JavaScriptObject)} on when the data has been loaded. 
    * @param callbackId DOM-Id to create the callback back. 
    */ 
    private native void createCallback(CrossSiteDomainRequest cscr, String callbackId) /*-{ 
     $wnd[callbackId] = function(j) { 
      [email protected]::onReceivedData(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)(callbackId, j); 
     }; 
    }-*/; 

    private native void removeCallback(String callbackId) /*-{ 
     $wnd[callbackId] = null; 
    }-*/; 

    public static native Element getHead() /*-{ 
     return $doc.getElementsByTagName('head')[0]; 
    }-*/; 

} 

如果創建一個CrossSiteDomainRequest對象爲URL http://www.test.com/loadXDR.js 你必須評估callbackId參數,並生成一個Java腳本,它可能看起來像這樣:

callbackId({"menu": { 
    "id": "file", 
    "value": "File", 
    "popup": { 
     "menuitem": [ 
      {"value": "New", "onclick": "CreateNewDoc()"}, 
      {"value": "Open", "onclick": "OpenDoc()"}, 
      {"value": "Close", "onclick": "CloseDoc()"} 
     ] 
    } 
}}); 

callbackId必須相應地被替換。