GET
请求,CORS支持所有类型的HTTP请求。JSONP的优势在于支持老式浏览器,以及可以向不支持CORS的网站请求数据。http://www.baidu.com#helloworld
的 "#helloworld" 就是 location.hash,改变 hash 值不会导致页面刷新,所以可以利用 hash 值来进行数据的传递,当然数据量是有限的。localhost:8080
下有文件 index.html 要和 localhost:8081
下的 data.html 传递消息,index.html 首先创建一个隐藏的 iframe,iframe 的 src 指向 localhost:8081/data.html
,这时的 hash 值就可以做参数传递。localhost:8080
域名下的一个代理 iframe 的 proxy.html 页面localhost:8080index.html
在请求数据端 localhost:8081/data.html
时,我们可以在该页面新建一个 iframe,该 iframe 的 src 指向数据端地址(利用 iframe 标签的跨域能力),数据端文件设置好 window.name 的值。http://localhost:8081/data.html
发送消息,然后监听 message,可以获得其文档发来的消息。http://www.example.com/index.html
和 http://sub.example.com/data.html
两个文件分别加上 document.domain = "example.com"
然后通过 index.html 文件创建一个 iframe,去控制 iframe 的 window,从而进行交互,当然这种方法只能解决主域相同而二级域名不同的情况,如果你异想天开的把 script.example.com 的 domain 设为 qq.com 显然是没用的,那么如何测试呢?sub1.example.com
和 sub2.example.com
这些域名地址指向本地 127.0.0.1:80
,然后用 nginx 做反向代理分别映射到 8080 和 8081 端口。sub1(2).example.com
等于访问 127.0.0.1:8080(1)