在web.xml中添加如下过滤器nginx
<!-- 若是nginx服务器中有跨域访问的配置,则须要注释掉如下跨域处理,不然须要添加如下的跨域配置 -->
<!-- 跨域配置start -->
<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, OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, If-Modified-Since, Last-Modified, Content-Length, Content-Range, Content-Disposition, Content-Description</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>
<!-- 跨域配置end -->web
ajax处理:ajax
$.ajax({ type: "GET", url: "http://localhost:8888/crbcHQimis/specialplanmanage/d7194c5a32ca43fc94684579ab42d3d6?userAcct=fansp", dataType: "json", success: function(data) { //..... } });
不过根据浏览器的保护规则,跨域的时候咱们建立的sessionId是不会被浏览器保存下来的,这样,当咱们在进行跨域访问的时候,咱们的sessionId就不会被保存下来,也就是说,每一次的请求,服务器就会觉得是一个新的人,而不是同一我的,为了解决这样的办法,须要在ajax请求时增长 xhrFields:{withCredentials:true},以下:json
$.ajax({ type: "GET", url: "http://localhost:8888/crbcHQimis/specialplanmanage/d7194c5a32ca43fc94684579ab42d3d6?userAcct=fansp", dataType: "json", xhrFields:{withCredentials:true}, success: function(data) { //..... } });