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了。

  • 相关阅读:
    3.14周末作业
    3.13作业
    文件处理
    字符编码
    基本数据类型总结
    基本数据类型--------------------集合set()
    python入门009
    作业009
    python入门008
    作业008
  • 原文地址:https://www.cnblogs.com/tzhz/p/3532193.html
Copyright © 2011-2022 走看看