问题1:用ajax请求数据时,出现了浏览器跨域问题。
解决跨域问题方法:用CORS解决
1. pom.xml
<!-- 跨域支持-->
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.5</version>
</dependency>
2. web.xml
<!-- 提供跨域支持 -->
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name> <!--配置授信的白名单的域名! -->
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
问题2:ajax跨域sessionid不一致
解决sessionid不一致方法:
jquery方式:在ajax里面加上红色字体部分
$.ajax({
url:"http://www.test.com/test/",
type:"post",
dataType:"json",
crossDomain: true,
xhrFields: {
withCredentials: true
},
success:function(data){
console.log(data);
}
});
vue需要再main.js的import下增加以下代码
axios.defaults.withCredentials = true