解决dwr跨域问题
程序员文章站
2024-01-07 08:30:10
...
有这样的需求 a.com 要通过 ajax 访问 b.com的数据
dwr 2.0提供了 跨域访问的功能
dwr写道
Cross Domain Ajax: <script> tag manipulation
Should you need to access servers in a different domain we've enabled a new remoting scheme. From DWR 2.0 you can use manipulation of <script> tags in addition to XMLHttpRequest or iframes. To use is you just need to do the following:
DWREngine.setMethod(DWREngine.ScriptTag);
Should you need to access servers in a different domain we've enabled a new remoting scheme. From DWR 2.0 you can use manipulation of <script> tags in addition to XMLHttpRequest or iframes. To use is you just need to do the following:
DWREngine.setMethod(DWREngine.ScriptTag);
dwr 写道
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.
<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.
实际开发
js 写道
<script type='text/javascript' src='http://a.com/dwr/interface/regutil.js'></script>
<script type='text/javascript' src='http://a.com/dwr/engine.js'></script>
<script type='text/javascript' src='http://a.com/dwr/util.js'></script>
<script type='text/javascript' src='http://a.com/dwr/engine.js'></script>
<script type='text/javascript' src='http://a.com/dwr/util.js'></script>
js 写道
DWREngine.setMethod(DWREngine.ScriptTag);
regutil._path = 'http://sso.dxy.cn/dwr/';
regutil._path = 'http://sso.dxy.cn/dwr/';
调用方法 出现 XMLHttpRequest.open 时权限不足
网上搜索了下 很多人说是 firefox的bug
再次搜索 看到scripttag 能搞定此问题
把js 的引入 改成 动态 加载 果然可以了 哈哈 。
注意点:dwr jar需要是2.0.2