ITeye博客

dwr跨域访问

lysming · 2010-10-29 07:18 · java · Web前端 · Markdown

使用dwr2.0.6版本,2.0版本应该都适用。

1)web.xml配置


<servlet>
	<servlet-name>dwr-invoker</servlet-name>
	<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>allowGetForSafariButMakeForgeryEasier</param-name>
			<param-value>true</param-value>
		</init-param>
		<!-- crossDomainSessionSecurity:设置成false能够从其他域进行请求 -->
		<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>
		<load-on-startup>1</load-on-startup>
	</servlet>

红色部分都必须配置。 各参数的说明:http://directwebremoting.org/dwr/server/servlet/index.html

2)页面调用


<script type='text/javascript' src='${ssoUrl}/dwr/interface/_ajax.js'></script>
<script type='text/javascript' src='http://sso.xxxx.com/dwr/engine.js'></script>
<script type='text/javascript' src='http://sso.xxxx.com/dwr/util.js'></script>

......

Remote._path = 'http://sso.xxxx.com/dwr/';
Remote.remoteMethod(params,{
     callback:function(value){

     },
     rpcType:dwr.engine.ScriptTag
});

另一种方式为在调用之前设置远程调用方式


DWREngine.setMethod(DWREngine.ScriptTag);
Remote._path = 'http://sso.xxxx.com/dwr/';
Remote.remoteMethod(params,{
     callback:function(value){

     }
});

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。