这几天按照公司要求搭建服务器(ssm+maven),因可能页面会和服务器分开部署以及用于app,在我测试的时候遇到html页面请求或者发送数据不能接受服务器返回值的问题,因为本人也算半生不熟,就看了了很多资料,也问了很多大牛,有用ajax+get+jsonp的,但是还是不行!终于在spring官网看到解决的办法:
这是我的测试文件
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="jquery-3.1.1.min.js"></script>
<script>
$(function () {
$('#Tes').click(function () {
$.ajax({
url:'http://localhost:8082/smart/user/reg.json?phone=13666668888&pwd=123456',
type:'post',
// dataType:'JSONP',
// 'contentType':'aplication/json',
success:function (re) {
$('#con').text("qweqweqwe");
alert(re.boolean);
},
error:function () {
alert("error");
}
});
});
});
</script>
</head>
<body>
<div id="con"></div>
<input type="button" id="Tes" value="测试"></input>
</body>
第一种spring以前的老办法:
编写一个类实现Filter,具体看图
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by WeiT on 2017/2/22.
* 用于跨域请求
*/
@Component
public class WebContextFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
httpServletResponse.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
httpServletResponse.setHeader("Access-Control-Allow-Credentials","true");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
之后配置web.xml
<filter>
<filter-name>contextFilter</filter-name>
<filter-class>com.smart.webapp.filter.WebContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>contextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
ok,启动服务器可以测试了!
第二种方法:
spring官网4.2版本以上的有cors的功能只要在spring的配置文件里面加上
<mvc:cors>
<mvc:mapping path="/**"
allowed-origins="*"
allow-credentials="true"
allowed-methods="GET,POST,OPTIONS"
max-age="3600"/>
</mvc:cors>
这只是我的个人设置,具体请按照自己的对照文档设置,好了就这样启动服务器,同样也成功了。
之后,我把两种方式同时都配置了测试,ok没问题!
注意:因为有的朋友喜欢在网上copy代码,有时候没注意一个问题,明明导入的是4.2以上的版本为什么没有cors呢!!!请看这里
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
so,注意你的配置抬头部分的是4.2以上的没!!!我的是4.3