(转)No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
来源:互联网 发布:java考试认证 编辑:程序博客网 时间:2024/06/06 01:20
原地址:http://blog.csdn.net/zhoucheng05_13/article/details/53580683
- 1
当使用ajax访问远程服务器时,请求失败,浏览器报如上错误。这是出于安全的考虑,默认禁止跨域访问导致的。
一、什么是跨域访问
举个栗子:在A网站中,我们希望使用Ajax来获得B网站中的特定内容。如果A网站与B网站不在同一个域中,那么就出现了跨域访问问题。你可以理解为两个域名之间不能跨过域名来发送请求或者请求数据,否则就是不安全的。跨域访问违反了同源策略,同源策略的详细信息可以点击如下链接:Same-origin_policy;
总而言之,同源策略规定,浏览器的ajax只能访问跟它的HTML页面同源(相同域名或IP)的资源。
二、解决方案
常用的解决方案有两种,可以分为客户端解决方案和服务器端解决方案。先说服务器端解决方案:
服务器端解决方案
在服务器端的filter或者servlet里面添加response.setHeader("Access-Control-Allow-Origin", "*");
“Access-Control-Allow-Origin”表示允许跨域访问,“*”表示允许所有来源进行跨域访问,这里也可以替换为特定的域名或ip。
很显然,这种方式对非网站拥有人员来说是不能做到的。而且此种方式很容易受到CSRF攻击。客户端解决方案
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
将ajax请求中的dataType属性设置为“jsonp”,jsonp是专门用来解决跨域访问而诞生的。
阅读全文
0 0
- (转)No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- ajax跨域请求:No 'Access-Control-Allow-Origin' header is present on the requested resource
- No 'Access-Control-Allow-Origin' header is present on the requested resource.ajax跨域请求
- js跨域访问,no 'access-control-allow-origin' header is present on the requested resource
- 本地Ajax跨域访问 No 'Access-Control-Allow-Origin' header is present on the requested resource.
- 跨域问题:No 'Access-Control-Allow-Origin' header is present on the requested resource.
- 跨域问题:“No 'Access-Control-Allow-Origin' header is present on the requested resource”
- jquery ajax No 'Access-Control-Allow-Origin' header is present on the requested resource
- ajax跨域(No 'Access-Control-Allow-Origin' header is present on the requested resource)
- No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore no
- (转)解决“No 'Access-Control-Allow-Origin' header is present on the requested resource”
- No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '***' is theref
- No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '
- No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin
- AJAX跨越、JS跨越请求 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin
- C#获取本机MAC地址和IP
- (基于UDP协议/tcp协议)socket客户端,服务端
- leetcode: 76. Minimum Window Substring
- Netty学习(二)—拆包粘包问题
- 图说2017年C++大会
- (转)No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- LSV云端上传下载矢量数据
- Ubuntu终端常用的快捷键
- leetcode: 77. Combinations
- kNN(K-Nearest Neighbor)算法简介
- tail命令及时查看文件变化
- Java输入输出流
- Self Organizing Maps 自映射人工神经网络
- CentOS6.5下安装ActiveMQ