No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
来源:互联网 发布:怎么申请淘宝账号小号 编辑:程序博客网 时间:2024/06/05 17:34
No 'Access-Control-Allow-Origin' headeris presentonthe requested resource.
--------------------------------------------------------------------------------------
当使用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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
将ajax请求中的dataType属性设置为“jsonp”,jsonp是专门用来解决跨域访问而诞生的。
- 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. 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
- 关于springmvc跨域请求 No 'Access-Control-Allow-Origin' header is present on the requested resource.
- JSCore的基本使用
- 修改默认的cmd路径
- 独石电容、瓷片电容、CBB电容、电解电容等各种电容相关知识
- 稳定排序
- Learning Curves
- No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- 剑指Offer----树的子结构
- ARKit 及 Core ML 等 7月份五大令人惊叹的 iOS 库
- spring注解开发
- Java中Arrays类进行数组排序
- Linux Shell及常用命令简介
- Get Revittypeselector
- 近期热门微信小程序demo源码下载汇总
- 1124: 两个有序数组合并