iframe跨域

简介:

ajax异步传输不能跨域获取数据!

这个时候怎么办呢?

可以通过iframe来拼接多个域中的页面,而各个域中的页面可以异步操作自己的数据内容,这样就实现了跨域操作的效果!

下面是我做的案例:

iframe主页面代码,

复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>企业级安全包</title>
<link rel="stylesheet" type="text/css" href="/css/default/softdown.css" />
</head>
<body>
    <div class="wrap">
    <div class="title"><span>企业级安装包</span></div>
    </div>
    <iframe src="http://test.ureading.com/ipa/ipa.html" frameborder=0 scrolling=no width="99%" height="480"></iframe>
    <iframe src="http://out.ureading.com/ipa/ipa.html" frameborder=0 scrolling=no width="99%" height="480"></iframe>
</body>
</html>
复制代码

这段代码,将两个域中的ipa.html文件整合到了一起,

各个ipa.html文件可以操作自己域中的内容。异步操作自己的数据。

下面来看看ipa.html中的内容!

复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>企业级安全包</title>
<link rel="stylesheet" type="text/css" href="/css/default/softdown.css" />
<script src="/js/class/jquery-1.3.2.min.js" type="text/javascript"></script>
<script>
    $(document).ready(function(){
        //进入页面就进行的处理
        $.ajax({
            type: "POST",
            url:"/default/index/ajaxcheckedlessons",
            data:"",
            success:function(response){
                if(response){
                    var data = eval('('+response+')');
                    //alert(data);
                    if(data['8yuwen']){
                      $("#8yuwen").attr("style","color:red;");
                    }else{
                      $("#8yuwen").attr("style","");  
                    }
                    if(data['8wuli']){
                      $("#8wuli").attr("style","color:red;");
                    }else{
                      $("#8wuli").attr("style","");  
                    }
                    if(data['9yuwen']){
                      $("#9yuwen").attr("style","color:red;");
                    }else{
                      $("#9yuwen").attr("style","");  
                    }
                    if(data['9wuli']){
                      $("#9wuli").attr("style","color:red;");
                    }else{
                      $("#9wuli").attr("style","");  
                    }
                }else{
                    alert("error");
                }
            }
        });    


        //ajax实现异步提醒
        $(".sellessons").click(function(){
            var todo = $(this).attr("todo");
            var class_id = $(this).attr("class_id");
            ajaxsellessons(todo,class_id);
            $(this).find('p').css('color', 'red');
            $(this).parent().siblings().find('p').css('color','black');
            //实现相反操作
            if(todo=='selyuwen'){
                todo='selwuli';
            }else{
                todo='selyuwen';
            }
            if(class_id==2){
                class_id=3;
            }else{
                class_id=2;
            }
            ajaxsellessons(todo,class_id);
            var $obj = $(".sellessons[class_id="+class_id+"][todo="+todo+"]");
            $obj.find('p').css('color', 'red');
            $obj.parent().siblings().find('p').css('color','black');
            
        });
        function ajaxsellessons(todo,class_id){
            $.ajax({
                type: "POST",
                url:"/default/index/"+todo,
                data:"class_id="+class_id,
                success:function(response){
                    if(response){
                        //alert(response);
                    }else{
                        alert("error");
                    }
                }
            });    
        }
    });
</script>
</head>

<body>
<div class="wrap">
  <div class="tab_box">
  <div class="tab_title">三楼对 <img src="/images/teacher/n_icon.png"> 演示版本</div>
  
  <table border="0" cellpadding="0" cellspacing="0" class="tab" width="100%">
  
  <tr>
    <td width="25%" align="center"><p><a href="http://test.ureading.com/r.php?name=student"><img src="/images/teacher/st_icon.png" width="61" height="72"><br/>学生</a></p></td>
    <td width="25%" align="center"><p><a href="http://test.ureading.com/r.php?name=teacher"><img src="/images/teacher/tc_icon.png" width="62" height="72"><br/>老师</a></p></td>
    <td width="50%" align="center">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <td width="10%" align="center">8班:</a>
            <td width="45%" align="left"><a todo="selyuwen" class_id="2" class="sellessons" style="cursor:pointer"><img src="/images/teacher/wl_icon.png" width="67" height="70"><p style="color:red;" id='8yuwen'>上语文</p></a></td>
            <td width="45%" align="left"><a todo="selwuli" class_id="2"class="sellessons" style="cursor:pointer"><img src="/images/teacher/wl_icon.png" width="67" height="70"><p style="color:red;" id='8wuli'>上物理</p></a></td>
        </tr>
        <tr>
            <td width="10%" align="center">9班:</a>
            <td width="45%" align="left"><a todo="selyuwen" class_id="3" class="sellessons" style="cursor:pointer"><img src="/images/teacher/wl_icon.png" width="67" height="70"><p style="color:red;" id='9yuwen'>上语文</p></a></td>
            <td width="45%" align="left"><a todo="selwuli" class_id="3" class="sellessons" style="cursor:pointer"><img src="/images/teacher/wl_icon.png" width="67" height="70"><p style="color:red;" id='9wuli'>上物理</p></a></td>
        </tr>
    </table>
    </td>
  </tr>
  <tr>
  <td colspan="3" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/default/clear/clearexams/op/todb" target="_blank"><img src="/images/teacher/pack.png" width="100" height="32" style="padding-top:0px;"></a>
  </td>
  </tr>
</table>
</div>
</body>
</html>
复制代码

这个页面通过ajax异步获取自己的一些数据。

这样两个域中的数据就可以通过一个页面来操作了。

这就是iframe的作用!

可以跨域操作!



本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/3171863.html,如需转载请自行联系原作者

相关文章
|
2月前
|
移动开发 前端开发 安全
iframe实现跨域通信的方法
iframe实现跨域通信的方法
55 6
|
Web App开发 JavaScript 安全
如何基于 iframe 解决跨域?
一般跨域听得比较多的方案是 Nginx 代理,CORS,而 JSONP 和 iframe 的跨域解决往往只在背八股文的时候出现,而且老是只给 JSONP 的实际操作手段,老是找不着 iframe 的实际操作,所以这篇文章就是介绍如何基于 iframe 解决跨域
|
Web App开发 移动开发 安全
「趣学前端」关于iframe跨域通信
用技术实现梦想,用梦想打开创意之门。之前开发遇到了iframe跨域通信的问题,今天分享一下解决方案,顺便总结一波知识点。
863 1
「趣学前端」关于iframe跨域通信
|
Web App开发 容器
你对iframe知道多少
你对iframe知道多少
|
JavaScript Java 前端开发
记一次<iframe>的使用
将jsp拆分frame框架,因为采用了第一种方式,一直在考虑用jquery异步请求获取数据,总是但不到效果, 终于在js写吐的时候选择了第二种方式。 //参考网上的使用,大多是下面这个样子,如果涉及静态页面之间定位,是没有问题的//href:为目标页面----->通过target定位到frame ${org.
1107 0
iframe跨域解决方案
    公司某个功能用的是iframe,由于跨域的原因,我们不能直接设置父级页面iframe的高度,所以用了一个中间页home来完成父级页面iframe的高度设置,这种中间页其实很多时候不好用,因为涉及到页面跳转和刷新,每次都得刷一下页面,而消息发送成功页的一个定位到顶部的功能,就是由于页面刷了一次导致体验不好,除了体验,这种中间页跳转的做法也很蹩脚和繁琐。
1434 0
关于iframe页面里的重定向问题
    最近公司做的一个功能,使用了iframe,父页面内嵌子页面,里面的坑还挺多的,上次其实就遇到过,只不过今天在此描述一下。     请允许我画个草图:          外层大圈是父级页面,里层是子级页面,我们是在父级引用子级页面的,由于是两个页面,URL肯定不一样的,“一般的,在子页面做的操作也仅仅对子页面生效”,现在我要说的就是关于这一点的!     公司这个功能,在子级页面完成某个操作以后,需要跳转页面,于是子级页面的代码是这样写的:提交=>提交成功=>跳转B页面。
4552 0
|
JavaScript 安全 前端开发