`
cat_rat
  • 浏览: 21104 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

javascript跨域问题 easyXDM

阅读更多

     最近在做项目的时候遇到了JavaScript跨域的问题,JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象。但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦。网上也有很多解决方案,没有找到适合的解决方法。最后使用了easyXDM解决了我的问题,使用easyXDM时有两个条件:1.浏览器要安装flash插件(具体支持的浏览器在说明文件里说的很清楚)2.要跨域的工程中都要引入easyXDM文件。希望能给需要的同学提供一个解决的方法。

       先说一下我的问题,项目涉及到两个工程,部署在不同的服务器上,http://127.0.0.1/a.jsp里嵌入一个iframeiframe引入了一个http://127.0.xx.xx/b.jsp页面,b.jsp里嵌了一个flex页面,我在b.jsp中有一个表单提交的saveInfo()js函数,我想在a.jsp里保存时调用saveInfo()

 

       具体的使用方法在example里都有很好的例子,照着做就可以。要注意引入easyXDM.jsjson2.js的引入。

下面是easyXDM的简要说明

http://stackoverflow.com/questions/10928245/cant-access-js-function-in-cross-domain-environment

[This is one of the best and easy to use APIs] available for Cross Domain Communication between web applications. easyXDM is easy to use, light weight, flexible, writing good quality code etc. I strongly think if you are going to continue with cross domain scenario, then you should adapt a robust cross domain apis such as easyXDM.

[easyXDM vs PostMessage Transport?] easyXDM will use the PostMessageTransport method if this feature is enabled by the browser such as (IE8+, Opera 9+, Firefox 3+, Safari 4+,Chrome 2+) on the other side it will use different transport methods for the un supported browsers such as (Firefox 1-2 - using the FrameElementTransport) other transport methods will be used as needed such as FlashTransport, NameTransport, and HashTransport).

 

This clearly makes easyXDM superior in terms on browser support specially the old browsers.

 

To demonstrate cross-domain access using easyXDM (Domain1 [abc.com] calls a method on a remote domain [xyz.com]):

 

<script type="text/javascript">
        /**
         * Request the use of the JSON object
         */
        easyXDM.DomHelper.requiresJSON("../json2.js");
    </script>
    <script type="text/javascript">
        var remote;
        window.onload = function(){
            /**
             * When the window is finished loading start setting up the interface
             */
            remote = new easyXDM.Interface(/** The channel configuration */{
                /**
                 * Register the url to hash.html, this must be an absolute path
                 * or a path relative to the root.
                 * @field
                 */
                local: "/hash.html",
                /**
                 * Register the url to the remote interface
                 * @field
                 */
                remote: "http://YOUR.OTHER.DOMAIN/YOUR_APPLICATION/YourRemoteApplication.html",
                /**
                 * Register the DOMElement that the generated IFrame should be inserted into
                 */
                container: document.getElementById("embedded")
            }, /** The interface configuration */ {
                remote: {
                    remoteApplicationMethod: {},
                    noOp: {
                        isVoid: true
                    }
                },
                local: {
                    alertMessage: {
                        method: function(msg){
                            alert(msg);
                        },
                        isVoid: true
                    }
                }
            },/**The onReady handler*/ function(){
                /**
                 * Call a method on the other side
                 */
                remote.noOp();
            });
        }

        function callRemoteApplicationMethod(Value1, Value2){
            remote.remoteApplicationMethod(Value1, Value2, function(result){
                alert("Results from remote application" + result);
            });
        }


    </script>

 In the body

<input type="button" onclick="callRemoteApplicationMethod(3,5)" value="call remoteApplicationMethod on remote domain"/>

 Now on your remote domain side you need to define your remote client as follows

*On Domain [ xyz.com ] - Remote domain*

This should go into the page YOUR_APPLICATION/YourRemoteApplication.html

 

   /**
         * Request the use of the JSON object
         */
        easyXDM.DomHelper.requiresJSON("../json2.js");
    </script>
    <script type="text/javascript">
        var channel, remote;
        /**
         * When the window is finished loading start setting up the channel
         */
        window.onload = function(){

            /**
             * When the channel is ready we create the interface
             */
            remote = new easyXDM.Interface(/** The channel configuration*/{}, /** The configuration */ {
                remote: {
                    alertMessage: {
                        isVoid: true
                    }
                },
                local: {
                    remoteApplicationMethod: {
                        method: doSomething(value1, value2){
                        // do somethigs with values

                        return "i'm return value from remote domain";
                        }
                    },
                    noOp: {
                        isVoid: true,
                        method: function(){
                            alert("Method not returning any data");
                        }
                    }
                }
            });
        }

 

       easyXDM地址

 

分享到:
评论

相关推荐

    easyXDM.zip_opera_跨域

    标题中的“easyXDM.zip_opera_跨域”暗示了这是一个关于JavaScript库easyXDM的压缩包,专门针对Opera浏览器的跨域通信问题。easyXDM(Easy Cross Domain Messaging)是一个开源JavaScript库,旨在解决Web应用程序中...

    跨域 iframe 实例 (Cross-Domain Javascript execution library)

    5. **工具的使用**:文章提到的“工具”可能是指一些库或框架,如jQuery的`$.ajax()`方法,它可以帮助简化跨域请求的处理,或者像EasyXDM这样的库,专门用于跨域通信。 6. **实际应用**:跨域iframe常用于广告嵌入...

    superagent-easyxdm:一个使用 easyXDM 而不是 XMLHttpRequest 的超级代理插件

    **超级代理插件superagent-easyxdm:跨域通信的新选择** 在现代Web开发中,跨域资源共享(CORS)已经成为一个常见的需求,尤其是在构建单页应用(SPA)时。传统的XMLHttpRequest(XHR)虽然支持跨域请求,但在某些...

    ahome-xdm:EasyXDM 的 Java(GWT) API (https

    【标题】"ahome-xdm:EasyXDM 的 Java(GWT) API (https)" 指的是一款基于Java和Google Web Toolkit(GWT)实现的API,它与流行的JavaScript库EasyXDM进行了集成,用于跨域通信解决方案。EasyXDM本身是一个JavaScript...

    javascriptRPC

    JavaScript社区有许多成熟的RPC框架,如Ajax-RPC、EasyXDM、Satellizer等,它们封装了上述通信细节,提供了更便捷的接口和错误处理机制。 在提供的文件"Test.java"中,虽然没有具体的内容,但我们可以假设这是...

    jquery-easyxdm-transport:一个 jQuery ajaxTransport,它通过 easyXDM 发出请求,因此您可以在 &lt;= IE9 中使用 CORS

    创建一个 jQuery ajaxTransport,它将使用一个 easyXDM.Rpc 对象来请求跨域资源。 为什么? 因为 IE9 不支持所有 CORS,所以有时发出请求的唯一方法是通过由 easyXDM 驱动的 iframe 代理,该代理可以代表您发出...

    让ie6 7 8 9支持html5 websocket

    7. 安全性和兼容性:注意跨域问题,确保Flash的crossdomain.xml文件允许WebSocket连接。同时,对于不同版本的Flash和WebSocket库,可能存在兼容性问题,需要调试和调整。 8. 用户体验:为了提高用户体验,可以考虑...

    wsoap:使用 SOAP 协议或 YII 框架的 Webservice 测试

    总结来说,"wsoap:使用 SOAP 协议或 YII 框架的 Webservice 测试"涉及到的关键知识点包括SOAP协议、YII框架的Web服务实现、WSDL描述、JavaScript中的SOAP客户端调用以及跨域问题的解决策略。理解并掌握这些内容对于...

Global site tag (gtag.js) - Google Analytics