跨域问题的解决思考

1.www.a.com

  a:a.com是主域

  b:www.a.com是二级域名

  c:对于主域相同,但是二级域名不同的问题,解决办法:通过设置document.domain来解决

    1:在www.a.com的a.html中设置documen.domain = 'a.com';然后通过a.html文件中创建一个iframe,链接到b.html中,插入到本界面,然后处理       

      document.domain = 'a.com';

      var ifr = document.createElement('iframe');

      ifr.src = 'http://script.a.com/b.html';

      ifr.style.display = 'none';

      document.body.appendChild(ifr);

      ifr.onload = function(){

          var doc = ifr.contentDocument || ifr.contentWindow.document;

          // 在这里操纵b.html

          alert(doc.getElementsByTagName("h1")[0].childNodes[0].nodeValue);

      };

    2.在script.a.com的b.html中设置documen.domain = 'a.com'

      document.domain = 'a.com';

posted on 2017-03-28 10:15  减肥的吉小胖  阅读(94)  评论(0)    收藏  举报