2011-06-29 102 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必须相应地被替换。