zoukankan      html  css  js  c++  java
  • 跨域读取Cookies(续)

    前面我们说的跨域读取Cookie,仅限于同一级域名情况。

    要跨域读取或设置Cookie,这种方法还有局限,我们可以用下面的方法来简单实现。

    <script type="text/javascript">
        function createScript(theme){
            var url = "http://xxx.xx.xx.xx/gzgx/themeStyle.jsp?theme="+theme;
            var script = document.createElement('script');
            script.id="remoteScriptLinkStyle";
            script.setAttribute('src', url);
            // 把script标签加入head,此时调用开始  
            document.getElementsByTagName('head')[0].appendChild(script); 
        }
        function setRomteCookie(theme){
            if(Ext.DomQuery.selectNode("script[id='remoteScriptLinkStyle']")==null){
                createScript(theme);
            }else{
                Ext.DomQuery.selectNode("script[id='remoteScriptLinkStyle']").remove();
                createScript(theme);
            }
        }
    </script>

    调用方法:

                setRomteCookie(subCook);

    在远程服务器端写一个处理themeStyle.jsp即可:

    <%
        String theme = request.getParameter("theme");
        System.out.println("theme:"+theme);
        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length > 0){
            for(int i=0;i<cookies.length;i++){
                Cookie cookie = cookies[i];
                if("theme".equals(cookie.getName())){
                    response.addCookie(cookie);
                }
            }
        }
        Cookie cookie = new Cookie("theme",theme);
        response.addCookie(cookie);
    %>

    这样就能够本地请求远程设置Cookie了。

  • 相关阅读:
    Search in Rotated Sorted Array
    排序
    Find Peak Element
    Search a 2D Matrix II
    Search a 2D Matrix
    Search for a Range
    nodejs编译sass模块包 node-compass,与gulp包gulp-sass使用方法
    canvas基础学习
    决定整理一下canvas的基础学习
    网页宽高clientWidth clientHeight获得数值不对的问题
  • 原文地址:https://www.cnblogs.com/tzhz/p/3532193.html
Copyright © 2011-2022 走看看