Skip to content

Instantly share code, notes, and snippets.

@zhaoda
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhaoda/4537afedfad411daaf5c to your computer and use it in GitHub Desktop.
Save zhaoda/4537afedfad411daaf5c to your computer and use it in GitHub Desktop.
ajax如何实现跨域请求
  1. 设置document.domain,增加隐藏的iframe做代理页面,只能解决主域相同而二级域名不同的情况,腾讯微博目前使用该方法;
  2. 利用iframelocation.hash,这个办法比较绕并且复杂,并且数据直接暴露在了url中,数据容量和类型都有限等;
  3. 服务器做Proxy代理,需要后端支持;
  4. 通过Script标签,即通常说的JSONP
  5. 使用window.name,name值在不同的页面(甚至不同域名)加载后依旧存在,并且可以支持非常长的 name 值(2MB),具体原理请参考http://www.planabc.net/2008/09/01/window_name_transport/

如果能说出第5个方案,说明很专业,知识面涉猎比较广。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment