There are a number of options on how remoting is handled by DWR, the method and verb should be transparent to the user, but might have different browser side-effects. Generally speaking DWR will pick the right method for you, but these options might be useful to get around unwanted browser side effects.
Supported by DWR 1.1 and above.
Do we ask the XHR object to be asynchronous? The default is true. Warning: This option will be ignored if you are using IFrame or ScriptTag remoting (see below) and is often generally a bad idea to change it to false because it can make your browser slow to respond.
To set the synchronisity on a global level:
dwr.engine.setAsync(true);
Or to set it on a call level:
Remote.method(params, { callback:function(data) { ... }, async:true });
Or to set it on a batch level:
dwr.engine.beginBatch(); Remote.method1(params, callback1); Remote.method2(params, callback2); dwr.engine.endBatch({ async:true });
Set the preferred remoting method. setRpcType()
does not guarantee that the selected method will be used, just that we will try that method first.
newmethod
must be one of dwr.engine.XMLHttpRequest
or dwr.engine.IFrame
, or, from 2.0 onwards you can also use dwr.engine.ScriptTag
. In DWR 1.0 this was called setMethod()
.
In version 3.0 (under development) this option is likely to be removed because the number of cases where DWR would override this setting was increasing and it was felt that it was giving the impression that it was more than just a hint. If you have reasons to need to set this manually, please contact the DWR mailing list.
XMLHttpRequest is the default and will work in most cases. IFrame is useful when ActiveX is turned off, although DWR will work this out and automatically switch to IFrame mode for you. ScriptTag is useful if you wish to get around cross-domain restrictions.
For example, to set the remoting method on a global level:
dwr.engine.setRpcType(dwr.engine.IFrame);
Or to set it on a call level:
Remote.method(params, { callback:function(data) { ... }, rpcType:dwr.engine.IFrame });
Or to set it on a batch level:
dwr.engine.beginBatch(); Remote.method1(params, callback1); Remote.method2(params, callback2); dwr.engine.endBatch({ rpcType:dwr.engine.IFrame });
ScriptTag remoting is useful when using DWR cross-domain. It is strongly recommended to ONLY use this system if you really need to. DWR protects you from a number of security attacks unless you are using it in cross-domain mode. As a general rule you should only use cross-domain scripting for totally public services.
To use script tag remoting, 2 bits of set-up are required - you need to configure the server to allow cross-domain requests, and to tell the client where the services are located.
To allow cross-domain script tag requests you need to add the following incantation to web.xml:
<init-param> <param-name>allowGetForSafariButMakeForgeryEasier</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>allowScriptTagRemoting</param-name> <param-value>true</param-value> </init-param>
To configure the client where to send cross-domain requests, set the ._path
variable for the remote interface in question:
Remote._path = 'http://otherdomain.com/webapp/dwr'; Remote.someFunction();
Cross-domain remoting may not work properly with reverse ajax.
This allows you to select between POST and GET, for both iframe and XMLHttpRequest methods. Some browsers (e.g. older versions of Safari) do not support XHR-POST so DWR will automatically switch to GET even if the POST verb is selected. So setHttpVerb()
ought to be treated as a hint only. In DWR 1.0 this was called setVerb()
.
In version 3.0 (under development) this option is removed because the number of cases where DWR would override this setting was increasing and it was felt that it was giving the impression that it was more than just a hint. If you really need to default to GET rather than POST you can do the following: dwr.engine.transport.xhr.httpMethod = 'GET';
(Note this is for version 3.0 and above only)
Setting the verb is not supported when using ScriptTag remoting.
For example, to set the remoting verb on a global level:
dwr.engine.setHttpVerb("GET");
Or to set it on a call level:
Remote.method(params, { callback:function(data) { ... }, httpVerb:"GET" });
Or to set it on a batch level:
dwr.engine.beginBatch(); Remote.method1(params, callback1); Remote.method2(params, callback2); dwr.engine.endBatch({ httpVerb:"GET" });