解决No 'Access-Control-Allow-Origin' header is present on the requested resource.跨域问题
跨域错误
错误原因
解决方法
在后台写一个过滤器过滤器来改写请求头头
CorsFilter.java
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest request = (HttpServletRequest)servletRequest;
String origin = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", origin);
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
response.setHeader("Access-Control-Allow-Credentials", "true");
String method = request.getMethod();
if(method.equalsIgnoreCase("OPTIONS")){
servletResponse.getOutputStream().write("Success".getBytes("utf-8"));
}else{
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
}
}
备注:
在web.xml文件中添加代码
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>xxx.xxx.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
---------------------
作者:汤姆猫丶
来源:CSDN
原文:https://blog.csdn.net/qq_39403545/article/details/82116121
版权声明:本文为博主原创文章,转载请附上博文链接!
解决No 'Access-Control-Allow-Origin' header is present on the requested resource.跨域问题的更多相关文章
- 解决No 'Access-Control-Allow-Origin' header is present on the requested resource.跨域问题(后台(java)解决方法)
附:前端常见跨域解决方案(全) 跨域错误 解决方法 在后台写一个过滤器来改写请求头 附上一个前端不知所以然的后台java代码: public class CorsFilter implements F ...
- WCF REST开启Cors 解决 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.
现象: 编写了REST接口: [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(UriTemp ...
- java、ajax 跨域请求解决方案('Access-Control-Allow-Origin' header is present on the requested resource. Origin '请求源' is therefore not allowed access.)
1.情景展示 ajax调取java服务器请求报错 报错信息如下: 'Access-Control-Allow-Origin' header is present on the requested ...
- 跨域问题解决----NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost:11000' is therfore not allowed access'
NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost ...
- has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
前端显示: has been blocked by CORS policy: Response to preflight request doesn't pass access control che ...
- .Net Core 处理跨域问题Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
网页请求报错: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Or ...
- Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fromHere.com' is therefore not allowed access.
Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is presen ...
- As.net WebAPI CORS, 开启跨源访问,解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource
默认情况下ajax请求是有同源策略,限制了不同域请求的响应. 例子:http://localhost:23160/HtmlPage.html 请求不同源API http://localhost:228 ...
- 解决跨域No 'Access-Control-Allow-Origin' header is present on the requested resource.
用angular发起http.get(),访问后端web API要数据,结果chrome报错:跨域了 Access to XMLHttpRequest at 'http://127.0.0.1:300 ...
随机推荐
- github二级域名配置
首先打开GitHub并登上你的GitHub账号 新建仓库 点击settings 接下来就是操作git往这个仓库存文件,该域名会访问index.html文件
- 菜鸟刷面试题(一、Java基础篇)
目录: JDK 和 JRE 有什么区别? == 和 equals 的区别是什么? 两个对象的 hashCode()相同,则 equals()也一定为 true,对吗? final 在 java 中有什 ...
- A:linux基础章节导航
本章的内容主要有: 模板机的安装 常用的小命令 find awk vim sed
- 系统重装/装Anaconda后,Windows开始菜单缺少快捷方式解决方案
系统重装后,想把D盘的软件添加快捷方式 以下以anaconda3为例,提供两种方法 方法一: 参考: 装Anaconda后,Windows开始菜单缺少快捷方式解决方案 方法二: 1. 添加环境变量 ...
- IDE开发小技巧-快速引包/替换关键词
快速引包 Ctrl+Shift+O 快速搜索/查找替换 Ctrl+F
- BOM的初级理解
1.什么是BOM,BOm有什么作用? BOM和DOM.ES是JavaScript的重要三个组成部分: 其中BOM是专门操作浏览器的API,其实他就是一种兼容性问题,这其中问题比较大就是IE浏览器,谁叫 ...
- AJAX-CORS 跨域
1.CORS就是一套AJAX跨域问题的解决方案. 2.CORS的原理: CORS定义一种跨域访问的机制,可以让AJAX实现跨域访问. 3.CORS浏览器支持情况: Chrome 3+ Firefox ...
- ASP.NET中使用附文本框插件
使用附文本选项框插件步骤 Newtonsoft.Json 改变js的配置文件的url 最后一定要关闭页面中的 ValidateRequest=false
- three.js实现世界地图城市迁徙图
概况如下: 1.THREE.CylinderGeometry,THREE.SphereGeometry绘制地图上的标记: 2.THREE.CanvasTexture用于加载canvas绘制的字体: 3 ...
- Ecplise设置全局编码为UTF-8
简介 Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简体中文操作系统 (Windows XP.Windows 2000简体中文)的缺省编码是GB18030,Windo ...