Web开发中的跨域(cross domain problem in web development)
2012-05-09 16:50
246 查看
1,什么是跨域?
要说跨域先说说“同源策略(same origin policy)”,同源策略是指网页上的脚本只能访问只能访问跟自己同源(host+protocol+port)的网页属性和方法。比如你在你的index.html中用iframe加载了http://www.google.com/index.html,那么http://www.google.com/index.html上的脚本就这能操作http://www.google.com上的内容。比如iframe内的脚本就不能访问iframe外的DOM。同样你也没有办法通过JavaScript来访问iframe里装载的页面。这样考虑主要是为了安全性,如果你进入的一个恶意网站,他将网银用iframe装载进来,然后在你输入的时候用JavaScript操作DOM获取iframe中网银输入的口令,密码,那就悲催了。
2,那该怎么办?
有时我们真的需要和其他网站通信,那该怎么办?如果是单向通信,你想从别的获取数据,并使用数据来改变当前的DOM,即显示数据,那么JSONP是个不错的选择。JSONP原理就是插入一个script标签,向另一个域请求js文件。比如:http://example.com?callback=showmsg。那么我们就在我们页面中插入:
<script src=”http://example.com?callback=showmsg”></script>
然后我们在定义callback函数
function showmsg(msg){ alert(msg); }
而exampe返回的是个json(javascript)
showmsg(“we are here”);
这使得,我们先前的回调函数得以执行。
如果你需要双向通信,目前有两个办法比较常用:
1,通过Flash加上crossdomain.xml来实现。
2,HTML5的postMessage。
Reference:
/article/4850965.html
http://easyxdm.net/wp/
相关文章推荐
- [FW] : Calling Cross Domain Web Services in AJAX
- some common used tips in web development
- 跨域策略文件crossdomain.xml
- Cross-Platform Development in C++: Building Mac OS X, Linux, and, Windows Applications (Paperback) Dec.2007.eBook-BBL
- Beginning POJOs: Lightweight Java Web Development Using Plain Old Java Objects in Spring, Hibernate,
- Inappropriate Uses in Web Development
- JSONP(cross domain) in jQuery.getJSON
- Flex快速入门系列之五:crossdomain.xml文件创建以及允许本地跨域访问远程资源
- Domain Driven Design and Development In Practice
- Silverlight Crossdomain Access WebService And Debug
- 关于跨域策略文件crossdomain.xml文件
- 使用crossdomain.xml让Flash可以跨域传输数据
- c# & Fizzler to crawl web page in a certain website domain
- Silverlight Crossdomain Access WebService And Debug
- Map Multiple Domain Names to Web Sites in IIS at a Single IP Address
- python cherrypy RESTful API,cherrypy支持跨域ajax访问,CROS,crossdomain ajax
- 『 论文阅读』A Multi-View Deep Learning Approach for Cross Domain User Modeling in Recommendation Systems
- JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls
- 跨域(cross-domain)访问 cookie (读取和设置)
- Rolling with developments in the Web component programming model