启用CORS实现Ajax跨域请求
2016-01-26 00:00
549 查看
应用场景:想从a.com请求b.com上的资源,由于同源策略不允许请求。
解决办法:在请求的php文件中加入
解决办法:在请求的php文件中加入
header("Access-Control-Allow-Origin: http://b.com");[/code] 这种比较安全,如果请求来源比较多,可以这么设置header("Access-Control-Allow-Origin: *");
如果使用Nginx,需要配置让其支持ajax跨越请求,参考网址location / { if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; # # Custom headers and headers various browsers *should* be OK with but aren't # add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; # # Tell client that this pre-flight info is valid for 20 days # add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } }
相关文章推荐
- Ubuntu LAMP 配置
- 根域名301到带www的域名的方式及检测方法
- Ubuntu 14.04 搭建LAMP
- LAMP环境由http转到https
- ECShop 去掉配送区域
- WordPress模板制作教程
- 帝国cms学习笔记
- 设置静态内容缓存时间
- PHP 用户地址定位
- IE兼容性问题集锦
- 颜色编码,css轮流变色
- why there are duplicated views in listview
- 关于MyEclipse的数据库Derby的问题
- java中context上下探微
- 在Spring3中使用注解(@Scheduled)创建计划任务
- XMLGregorianCalendar和java.util.Date的相互转化
- Maple-注册码-激活码汇总
- 为什么要加入wemall扶持计划?
- EDIUS中的视频如何实现批量输出
- Bootstrap 表格