---
title: "dwr跨域访问"
author: "lysming"
date: "2010-10-29T07:18:25Z"
tags: []
categories: ["Web前端"]
slug: "dwr"
draft: false
lastmod: "2010-10-29T07:18:25Z"
id: 797191
url: "https://lysming.iteye.com/blog/797191"
---

使用dwr2.0.6版本，2.0版本应该都适用。

1）web.xml配置

```java

<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）页面调用

```java

<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
});

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

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

```