@RequestMapping(value = "/login.do",method = RequestMethod.POST)
public Message login(HttpServletRequest request,HttpServletResponse response) throws BusinessException {
// CORSUtil.addHeaders(response);
// 获取请求参数
JSONObject json = WebUtils.getRequest(request);
String userName = (String) json.get("userName");
String password = (String) json.get("password");
// String userName = request.getParameter("userName");
// String password = request.getParameter("password");
UsersLoginInfoVO usersLoginInfo = iLoginService.login(userName, password);
return response(SUCCESS.getCode(),usersLoginInfo);
}
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class HeadersCORSFilter implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub } @Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpResp = (HttpServletResponse) response;
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
httpResp.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS");
httpResp.setHeader("Access-Control-Allow-Origin", "*");
httpResp.setHeader("Access-Control-Allow-Credentials","true");
httpResp.setHeader("Access-Control-Max-Age", "3600");
if (httpServletRequest.getMethod().equalsIgnoreCase("OPTIONS")) {
httpResp.setHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,accept,Origin," +
"Access-Control-Request-Method,Access-Control-Request-Headers,userID,Authentication");
} chain.doFilter(request, response);
} @Override
public void destroy() {
// TODO Auto-generated method stub } }
 <!--&lt;!&ndash;cors过滤器&ndash;&gt;-->
<filter>
<filter-name>cors</filter-name>
<filter-class>com.backstage.filter.HeadersCORSFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>新闻动态</title>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
</head> <body class="body-blackBg">
</body>
<script src="jquery.min.js"></script>
<script> $(document).ready(function () {
$.ajax({
type: "POST",
dataType: "json",
url: "http://localhost:8081/api/pas/web/user/login.do",
contentType: "application/json; charset=utf-8",
data:JSON.stringify({userName:"admin", password:"admin123"}),
success: function (data) {
console.log(data);
},
error: function (data) { }
}); }) </script>
</html>

spring mvc跨域(ajax post json)--filter方案的更多相关文章

  1. spring mvc跨域(post)--filter方案

    import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...

  2. 关于Spring MVC跨域

    1.Sping MVC 3.X跨域 关于跨域问题,主要用的比较多的是cros跨域. 详细介绍请看https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Ac ...

  3. spring mvc 跨域问题。。。解决

    官方推荐方式: http://spring.io/blog/2015/06/08/cors-support-in-spring-framework 方式1: $.ajax({ //前台:常规写法.注意 ...

  4. spring mvc跨域设置(全局)

    //--------------第一步//spring 5版本全局配置方式 @Configuration @EnableWebMvc public class SpringMvcBeans imple ...

  5. spring mvc 跨域请求处理——spring 4.2 以上

    Controller method CORS configuration You can add to your @RequestMapping annotated handler method a  ...

  6. asp net core mvc 跨域ajax解决方案

    1.配置服务端 在Startup文件中国配置Cors策略: IEnumerable<Client> clients= Configuration.GetSection("Clie ...

  7. JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案

    JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案 最近在开发WSS RESTful服务的时候, 碰到了这些个纠 ...

  8. 【HTTP header】【Access-Control-Allow-Credentials】跨域Ajax请求时是否带Cookie的设置

    1. 无关Cookie跨域Ajax请求 客户端 以 Jquery 的 ajax 为例: $.ajax({ url : 'http://remote.domain.com/corsrequest', d ...

  9. 跨域Ajax请求时是否带Cookie的设置

    1. 无关Cookie跨域Ajax请求 客户端 以 Jquery 的 ajax 为例: $.ajax({ url : 'http://remote.domain.com/corsrequest', d ...

随机推荐

  1. windows下大文件SQL导入问题

    今天遇到了一个比较棘手的问题,SQL文件太大,导入数据库时太过于麻烦,超过1G以后nodpad.sublime等编辑器都打不开,这就有了一个比较尴尬的问题. 因为是本地测试所以环境是windows,最 ...

  2. 温故而知新—heap

    堆:堆不是STL中的容器组件,堆有分为大根堆和小根堆,堆的底层实现可以用优先队列进行实现.底层的容器实际上是一个vector.在C++数据结构中,堆也可用数组来实现.对于使用C++的开发人员来说,st ...

  3. 使用Angular CLI进行单元测试和E2E测试

    第一篇文章是: "使用angular cli生成angular5项目" : http://www.cnblogs.com/cgzl/p/8594571.html 第二篇文章是: & ...

  4. MySQL常用的查询命令

    MySQL常用的查询命令 author: headsen chen   2017-10-19  10:15:25 个人原创.转载请注明作者,出处,否则依法追究法律责任 1,查询现在的时间:mysql& ...

  5. Eclipse CDT开发环境搭建及问题记录(Windows)

    这两天在整Eclipse,在此记录过程中遇到的一些问题. 1.安装JDK,配置系统路径: 2.下载Eclipse 直接访问Eclipse官网(https://www.eclipse.org/downl ...

  6. 001-List,数组,Set,Map属性的映射

    hibernate.cfg.xml: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configur ...

  7. Java 后端微信支付demo

    Java 后端微信支付demo 一.导入微信SDK 二.在微信商户平台下载证书放在项目的resources目录下的cert文件夹下(cert文件夹需要自己建) 三.实现微信的WXPayConfig接口 ...

  8. 【Python】 MySQLdb的安装与使用

    MySQLdb MySQLdb是一款较为底层的,python连接mysql用的模块.和更加高级的,提供ORM的模块不同,MySQLdb主要还是聚焦于如何和数据库进行连接和进行基本的操作,操作的体现形式 ...

  9. 【Python】正则表达式re

    re 正则表达式(regular expression)这玩意儿多nb就不用说了,python用re模块来支持正则 首先是一些正则表达式的概念 1. 通配符 . 2. 多字符选择 [...] [abc ...

  10. linux No space left on device 由索引节点(inode)爆满引发500问题

    inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统读取 ...