1. 什么是跨域

简单地理解就是因为JavaScript同源策略的限制,a.com 域名下的js无法操作b.com或是c.a.com域名下的对象。

同源是指相同的协议、域名、端口。特别注意两点:

  • 如果是协议和端口造成的跨域问题“前台”是无能为力的,
  • 在跨域问题上,域仅仅是通过“协议+域名+端口”来识别,两个不同的域名即便指向同一个ip地址,也是跨域的。

2. 跨域解决方案

跨域解决方案有多种,大多是利用JS Hack:

3. CORS

CORS: 跨域资源共享(Cross-Origin Resource Sharing)http://www.w3.org/TR/cors/

当前几乎所有的浏览器(Internet Explorer 8+, Firefox 3.5+, Safari 4+和 Chrome 3+)都可通过名为跨域资源共享(Cross-Origin Resource Sharing)的协议支持ajax跨域调用。(see: http://caniuse.com/#search=cors)

Chrome, Firefox, Opera and Safari 都使用的是 XMLHttpRequest2 对象, IE使用XDomainRequest。XMLHttpRequest2的Request属性:open()、setRequestHeader()、timeout、withCredentials、upload、send()、send()、abort()。

XMLHttpRequest2的Response属性:status、statusText、getResponseHeader()、getAllResponseHeaders()、entity、overrideMimeType()、responseType、response、responseText、responseXML。

启用 CORS 请求

假设您的应用已经在 example.com 上了,而您想要从 www.example2.com 提取数据。一般情况下,如果您尝试进行这种类型的 AJAX 调用,请求将会失败,而浏览器将会出现“源不匹配”的错误。利用 CORS,www.example2.com 服务端只需添加一个HTTP Response头,就可以允许来自 example.com 的请求:

  1. Access-Control-Allow-Origin: http://example.com
  2. Access-Control-Allow-Credentials: true(可选)

可将 Access-Control-Allow-Origin 添加到某网站下或整个域中的单个资源。要允许任何域向您提交请求,请设置如下:

  1. Access-Control-Allow-Origin: *
  2. Access-Control-Allow-Credentials: true(可选)

其实,该网站 (html5rocks.com) 已在其所有网页上均启用了 CORS。启用开发人员工具后,您就会在我们的响应中看到 Access-Control-Allow-Origin 了。

提交跨域请求

如果服务器端已启用了 CORS,那么提交跨域请求就和普通的 XMLHttpRequest 请求没什么区别。例如,现在 example.com 可以向 www.example2.com 提交请求了:

  1. var xhr = new XMLHttpRequest();
  2. // xhr.withCredentials = true; //如果需要Cookie等
  3. xhr.open('GET', 'http://www.example2.com/hello.json');
  4. xhr.onload = function(e) {
  5. var data = JSON.parse(this.response);
  6. ...
  7. }
  8. xhr.send();

4. 服务端Nginx配置

要实现CORS跨域,服务端需要这个一个流程:http://www.html5rocks.com/static/images/cors_server_flowchart.png

对于简单请求,如GET,只需要在HTTP Response后添加Access-Control-Allow-Origin。

对于非简单请求,比如POST、PUT、DELETE等,浏览器会分两次应答。第一次preflight(method: OPTIONS),主要验证来源是否合法,并返回允许的Header等。第二次才是真正的HTTP应答。所以服务器必须处理OPTIONS应答。

http://enable-cors.org/server_nginx.html这里是一个nginx启用COSR的参考配置。

流程如下:

  1. 首先查看http头部有无origin字段;
  2. 如果没有,或者不允许,直接当成普通请求处理,结束;
  3. 如果有并且是允许的,那么再看是否是preflight(method=OPTIONS);
  4. 如果是preflight,就返回Allow-Headers、Allow-Methods等,内容为空;
  5. 如果不是preflight,就返回Allow-Origin、Allow-Credentials等,并返回正常内容。

用伪代码表示:

  1. location /pub/(.+) {
  2. if ($http_origin ~ <允许的域(正则匹配)>) {
  3. add_header 'Access-Control-Allow-Origin' "$http_origin";
  4. add_header 'Access-Control-Allow-Credentials' "true";
  5. if ($request_method = "OPTIONS") {
  6. add_header 'Access-Control-Max-Age' 86400;
  7. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';
  8. add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type';
  9. add_header 'Content-Length' 0;
  10. add_header 'Content-Type' 'text/plain, charset=utf-8';
  11. return 204;
  12. }
  13. }
  14. # 正常nginx配置
  15. ......
  16. }

但是由于Nginx 的 if 是邪恶的,所以配置就相当地让人不爽(是我的配置不够简洁吗?)。下面nginx-spdy-push里/pub接口启用CORS的配置:

  1. # push publish
  2. # broadcast channel name must start with '_'
  3. # (i.e., normal channel must not start with '_')
  4. # GET /pub/channel_id -> get statistics about a channel
  5. # POST /pub/channel_id -> publish a message to the channel
  6. # DELETE /pub_admin?id=channel_id -> delete the channel
  7. #rewrite_log on;
  8. # server_name test.gw.com.cn
  9. # listen 2443 ssl spdy
  10. location ~ ^/pub/([-_.A-Za-z0-9]+)$ {
  11. set $cors "local";
  12. # configure CORS based on https://gist.github.com/alexjs/4165271
  13. # (See: http://www.w3.org/TR/2013/CR-cors-20130129/#access-control-allow-origin-response-header )
  14. if ( $http_origin ~* "https://.+\.gw\.com\.cn(?=:[0-9]+)?" ) {
  15. set $cors "allow";
  16. }
  17. if ($request_method = "OPTIONS") {
  18. set $cors "${cors}options";
  19. }
  20. # if CORS request is not a simple method
  21. if ($cors = "allowoptions") {
  22. # Tells the browser this origin may make cross-origin requests
  23. add_header 'Access-Control-Allow-Origin' "$http_origin";
  24. # in a preflight response, tells browser the subsequent actual request can include user credentials (e.g., cookies)
  25. add_header 'Access-Control-Allow-Credentials' "true";
  26. # === Return special preflight info ===
  27. # Tell browser to cache this pre-flight info for 1 day
  28. add_header 'Access-Control-Max-Age' 86400;
  29. # Tell browser we respond to GET,POST,OPTIONS in normal CORS requests.
  30. # Not officially needed but still included to help non-conforming browsers.
  31. # OPTIONS should not be needed here, since the field is used
  32. # to indicate methods allowed for 'actual request' not the preflight request.
  33. # GET,POST also should not be needed, since the 'simple methods' GET,POST,HEAD are included by default.
  34. # We should only need this header for non-simple requests methods (e.g., DELETE), or custom request methods (e.g., XMODIFY)
  35. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';
  36. # Tell browser we accept these headers in the actual request
  37. add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type';
  38. # === response for OPTIONS method ===
  39. # no body in this response
  40. add_header 'Content-Length' 0;
  41. # (should not be necessary, but included for non-conforming browsers)
  42. add_header 'Content-Type' 'text/plain, charset=utf-8';
  43. # indicate successful return with no content
  44. return 204;
  45. }
  46. if ($cors = "allow") {
  47. rewrite /pub/(.*) /pub_cors/$1 last;
  48. }
  49. if ($cors = "local") {
  50. rewrite /pub/(.*) /pub_int/$1 last;
  51. }
  52. }
  53. location ~ /pub_cors/(.*) {
  54. internal;
  55. # Tells the browser this origin may make cross-origin requests
  56. add_header 'Access-Control-Allow-Origin' "$http_origin";
  57. # in a preflight response, tells browser the subsequent actual request can include user credentials (e.g., cookies)
  58. add_header 'Access-Control-Allow-Credentials' "true";
  59. push_stream_publisher admin; # enable delete channel
  60. set $push_stream_channel_id $1;
  61. push_stream_store_messages on; # enable /sub/ch.b3
  62. push_stream_channel_info_on_publish on;
  63. }
  64. location ~ /pub_int/(.*) {
  65. # internal;
  66. push_stream_publisher admin; # enable delete channel
  67. set $push_stream_channel_id $1;
  68. push_stream_store_messages on; # enable /sub/ch.b3
  69. push_stream_channel_info_on_publish on;
  70. }

5. 客户端javascript代码

下面是https://spdy.gw.com.cn/sse.html里的代码

  1. var xhr = new XMLHttpRequest();
  2. //xhr.withCredentials = true;
  3. xhr.open("POST", "https://test.gw.com.cn:2443/pub/ch1", true);
  4. // xhr.setRequestHeader("accept", "application/json");
  5. xhr.onload = function() {
  6. $('back').innerHTML = xhr.responseText;
  7. $('ch1').value = + $('ch1').value + 1;
  8. }
  9. xhr.onerror = function() {
  10. alert('Woops, there was an error making the request.');
  11. };
  12. xhr.send($('ch1').value);

页面的域是https://spdy.gw.com.cn, XMLHttpRequest的域是https://test.gw.com.cn:2443,不同的域,并且Post方式。

用Chrome测试,可以发现有一次OPTIONS应答。如过没有OPTIONS应答,可能是之前已经应答过,被浏览器缓存了'Access-Control-Max-Age' 86400;,清除缓存,再试验。

6. 相关链接

Nginx CORS实现JS跨域的更多相关文章

  1. Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持

    Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持 1. 设置 document.domain为一致  推荐1 2. Apache 反向代理 推荐1 ...

  2. js跨域请求解决方案

    什么是跨域? 跨域是指一个域下的文档或脚本试图去请求另一个域下的资源,这里跨域是广义的. 广义的跨域: 1.) 资源跳转: A链接.重定向.表单提交 2.) 资源嵌入: <link>.&l ...

  3. 前端Js跨域方法汇总—剪不断,理还乱,是跨域

    1.通过jsonp跨域2.通过修改document.domain来跨子域(iframe)3.隐藏的iframe+window.name跨域4.iframe+跨文档消息传递(XDM)5.跨域资源共享 C ...

  4. 【前端】【转】JS跨域问题总结

    详情见原博客:详解js跨域问题 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 跨域资源共享(CORS) CORS(Cross-Origin Resource Sharing)跨域资源 ...

  5. CORS 协议(跨域资源共享)

      跨域问题 只要协议.域名.端口有任何一个不同,都被当作是不同的域.   为什么会有跨域的限制? 之前发生过的一些跨域安全事件: 新浪微博XSS受攻击事件 2011年6月28日晚,新浪微博出现了一次 ...

  6. JS跨域两三事

    今日,前端开发要求新的Web服务需要支持跨域,因为要发起 Ajax 到后端web 服务域名请求数据: 前端application域名是 other.abc.com (举个栗子)  api接口域名是 a ...

  7. SpringBoot入门教程(十三)CORS方式实现跨域

    什么是跨域?浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域 . 跨域资源访问是经常会遇到的场景,当一个资源从与该资源本身所在的服务器不同的域或端口请求一个资源时,资 ...

  8. 基于CORS的GeoServer跨域访问策略

    GeoServer的跨域访问问题,有多种解决方法,本文介绍一种基于CORS的GeoServer跨域访问方法. CORS简介 CORS是一个W3C标准,全称是"跨域资源共享"(Cro ...

  9. 什么是JS跨域请求

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

随机推荐

  1. Oracle 客户端配置笔记

    1.右击桌面的我的电脑 -> 高级 -> 环境变量,新建 1) 变量名:ORACLE_HOME 变量值:D:\app\instantclient_11_2 2) 变量名:TNS_ADMIN ...

  2. nginx gzip on

    # Gzip settings. gzip on; gzip_http_version 1.0;默认值是1.1 gzip_comp_level ; #压缩级别,1压缩比最小处理速度最快,9压缩比最大但 ...

  3. git创建分支与合并分支

    git branch myfeture 创建分支 git checkout myfeture git add --all git commit -m git push origin myfeture ...

  4. 表单元素的submit()方法和onsubmit事件(转)

    1.表单元素中出现了name="submit"的元素 2.elemForm.submit();不会触发表单的onsubmit事件 3.动态创建表单时遇到的问题 表单元素拥有subm ...

  5. 《Pointers On C》读书笔记(第二章 基本概念)

    1.从源代码到生成可执行程序的过程整体上可以分为两个阶段:编译和链接.其中,编译过程大致上又可分为:预处理.编译和汇编.预处理阶段主要对源代码中的预处理指令(包含宏定义指令<如 #define& ...

  6. Spring 装配Bean

    Spring 装配Bean 装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介 ...

  7. DebugView使用技巧

    DebugView 可以很方便的捕获系统实时输出的Debug信息,并保存为日志文件.可以远程捕获服务器上的Debug信息. 比较方便开发人员在系统发布前监控一些系统流程和异常,甚至在系统不大的情况下, ...

  8. 再探Delphi2010 Class的构造和析构顺序

    发了上一篇博客.盒子上有朋友认为Class的构造和析构延迟加载.是在Unit的初始化后调用的Class的构造.在Unit的反初始化前调用的Class的析构函数. 为了证明一下我又做了个试验 unit ...

  9. Spring Boot MyBatis 连接数据库

    最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...

  10. BZOJ 3498 PA2009 Cakes(三元环处理)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3498 [题目大意] N个点m条边,每个点有一个点权a. 对于任意一个三元环(j,j,k ...