首页 技术 正文
技术 2022年11月9日
0 收藏 526 点赞 4,384 浏览 888 个字

在提供api给其它应用使用时,有时我们会要限制它的跨域使用,而有时,我们又要用CORS来打破AJAX只能同源使用的限制

跨域资源共享 CORS 详解 – 阮一峰的网络日志
http://www.ruanyifeng.com/blog/2016/04/cors.html

总结:
1、浏览器发出CORS请求,即在访问时http头里增加一个Origin字段
2、服务端判断Origin,然后在返回的http头里增加Access-Control-Allow-Origin、Access-Control-Allow-Credentials字段
3、JSONP只支持GET,CORS支持所有,但JSONP支持老浏览器,并且不需要服务端支持

Nginx通过CORS实现跨域 – OPEN 开发经验库
http://www.open-open.com/lib/view/open1472737324822.html

Nginx CORS实现JS跨域 – oyzl68的专栏 – 博客频道 – CSDN.NET
http://blog.csdn.net/oyzl68/article/details/18741057

nginx允许所有二级域名跨域请求 – 曾健生的专栏 – 博客频道 – CSDN.NET
http://blog.csdn.net/newjueqi/article/details/51385657

nginx配置文件里支持CORS很简单,但是如果支持多个指定网站,或者支持正则匹配,则有几种写法,以下我在用的一种:

map $http_origin $corsHost {
    default       0;
    “~http://a.a.cn” $http_origin;
    “~https://b.b.b.com” $http_origin;
    “~https?://(.*).c.com” $http_origin;
    }
server {
    …
    add_header ‘Access-Control-Allow-Origin’ $corsHost;
    add_header Access-Control-Allow-Methods GET,POST;
    …

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918