cors 跨域
来源:互联网 发布:天猫比淘宝有什么优势 编辑:程序博客网 时间:2024/05/22 07:50
简单记录:
1.客户端(简单的ajax就可以):
$.ajax({
url : '',// 跨域访问
type: "GET",
async: true,
success : function(a,b,c){
console.log('成功'+a)
},
error:function(a,b,c){
console.log('失败'+a)
}
});
2.服务器端:
添加一个filter:
public class CORSFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-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");
chain.doFilter(req, res);
}
@Override
public void destroy() {
}
}
在web.xml中进行配置:
<!-- cors跨域设置 -->
<filter>
<filter-name>cors</filter-name>
<filter-class>com.hsms.filter.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- CORS跨域
- 跨域 CORS
- cors 跨域
- 跨域-CORS
- cors 跨域
- 跨域CORS
- cors跨域
- CORS跨域
- 跨域CORS
- 跨域cors
- node跨域cors模块
- JavaScript 跨域(CORS)
- ajax之cors跨域
- 关于配置cors跨域
- MVC跨域CORS扩展
- nodejs 跨域CORS 学习
- CORS解决ajax跨域
- 跨域-JSONP/CORS
- 一个server启两个实例引发的问题
- linux awk 内置变量使用介绍
- char类型与Unicode的编码
- 4336: BJOI2015 骑士的旅行
- linux awk数组操作详细介绍
- cors 跨域
- CSS white-space 属性
- 《剑指offer》4:替换空格
- Android开发之ListView的使用
- awk 字符串连接操作(字符串转数字,数字转字符串)
- Nginx做反向代理
- PAT 1049Recover the Smallest Number (30)
- PAT乙级(Basic Level)1002(C++)
- Lombok消除冗长的Java代码