Ajax requests will have cross domain problems. The front page reports the following error:

 Access to XMLHttpRequest at 'xxxx' from origin 'xxxx' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

 A method of Java backend to solve cross domain problems

1、 What is cross domain?
Simply understood, because of the restriction of JavaScript homology policy, js under a.com domain name cannot operate objects under b.com or c.a.com domain name.
Same source refers to the same protocol, domain name and port. Pay special attention to two points:
If the cross domain problem caused by the protocol and port is "foreground", there is nothing we can do,
On the cross domain issue, domains are only identified by "protocol+domain name+port". Two different domain names are cross domain even if they point to the same IP address.

2、 Common cross domain situations

 URL indicates whether communication is allowed http://www.a.com/a.js http://www.a.com/b.js Allowed under the same domain name http://www.a.com/lab/a.js http://www.a.com/script/b.js Allow different folders under the same domain name http://www.a.com:8000/a.js http://www.a.com/b.js The same domain name is not allowed on different ports http://www.a.com/a.js https://www.a.com/b.js The same domain name is not allowed by different protocols http://www.a.com/a.js http://192.168.1.1/b.js Domain name and corresponding IP address are not allowed http://www.a.com/a.js http://script.a.com/b.js Same primary domain, different sub domains are not allowed http://www.a.com/a.js http://a.com/b.js The same domain name and different secondary domain names (as above) are not allowed (cookies are also not allowed to access in this case) http://www.cnblogs.com/a.js http://www.a.com/b.js Different domain names are not allowed

The specific solutions are as follows for reference only:
1) Add a filtering method with the following code:

 package com.lcry.filter; import javax.servlet.*; import javax.servlet.http. HttpServletResponse; import java.io.IOException; /** * @author lcry * @create 2019/1/4 */ public class SessionListener  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 httpServletResponse=(HttpServletResponse) response; httpServletResponse.setHeader("Access-Control-Allow-Origin", "*"); chain.doFilter(request, response); } @Override public void destroy() { // TODO Auto-generated method stub } }

2) Configuration web.xml , add interception

 <!-- Solving cross domain problems --> <filter> <filter-name>SessionListener</filter-name> <filter-class>com.lcry.filter. SessionListener</filter-class> </filter> <filter-mapping> <filter-name>SessionListener</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

This can solve the ajax cross domain problem.