0
Q
GWT跨域rpc
A
回答
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
必須相應地被替換。
相關問題
- 1. 使用GWT跨域請求
- 2. gwt grails跨域請求
- 3. RPC 1.0 vs deRPC GWT-RPC
- 4. GWT RPC失敗
- 5. 錯誤gwt-rpc
- 6. Gwt和php rpc
- 7. GWT RPC SerializationException
- 8. Google GWT RPC Vector
- 9. GWT RPC Alternative
- 10. Gwt RPC和Servlet
- 11. 繼承+ RPC GWT
- 12. GWT/GWT-RPC使用com.google.gdata.contacts
- 13. GWT RPC屬性空
- 14. GWT RPC不工作
- 15. GWT RPC XSRF保護
- 16. GWT RPC GWTTestCase + GUICE 2.0
- 17. Vaadin GWT RPC設置?
- 18. GWT-One RPC之後
- 19. GWT SerializationException__Ljava_lang_String_2V做RPC時
- 20. GWT RPC序列化
- 21. GWT從跨域接收xml作爲JSONP
- 22. GWT + Phonegap跨域請求無效
- 23. GWT和第三方跨域JavaScript
- 24. gwt-RPC問題!什麼是使用gwt-RPC的最佳實踐?
- 25. GWT,Guice ServletModule和GWT RPC問題與servlet.init()
- 26. 創建GWT RPC服務
- 27. 尋找工作GWT RPC庫
- 28. 將GWT RPC轉換爲RestyGWT
- 29. @WebServlet註釋和GWT RPC
- 30. GWT RPC回調調用GWTTestCase