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