This method only supports versions spring4.2 and above
First, the problem description
A and B systems, a system wants to invoke the backend method in B system via Ajax (b system Springmvc)
Second, problem solving
Ajax in 1.A system:
var str = "{' id ': ' fundcode ', ' '": ' 000311 '} "; Jquery.ajax ({URL:' Http://172.88.88.888:8180/test/app/product/public/getManaAndComp ', type:"POST", DataType:"Text", data:{"param": str, "Osflag": ' 3 '}, Async:true,success:function (serverinfo) {console.log (serverinfo); var Data=Base64.decode (ServerInfo); Data=Json.parse (Data); Console.log (Data); }, //Success Enderror:function () {Console.log (' Error '); } });
2.B System Configuration Simply configure the filter
Xml
<filter> <filter-name>cors</filter-name> <filter-class> com.datangwealth.common.filter.corsconfig</filter-class> </filter> < filter-mapping> <filter-name>cors</filter-name> <url-pattern>/*</ url-pattern> </filter-mapping>
Corsconfig.java
PackageCom.datangwealth.modules.monitor.service;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.stereotype.Component;ImportOrg.springframework.web.servlet.config.annotation.CorsRegistry;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; Public classCorsconfigImplementsFilter {@Override Public voiddestroy () {} @Override Public voidDoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filterchain)throwsIOException, servletexception {httpservletresponse response=(HttpServletResponse) servletresponse; HttpServletRequest Request=(httpservletrequest) ServletRequest; Response.setheader ("Access-control-allow-origin", "*"); String token= Request.getheader ("token"); System.out.println ("Filter Origin:" +token);//printing, you can see a non-simple request, will be filtered two times, that is, the request two times, the first request to confirm compliance with cross-domain requirements (preflight), this time is a custom information without headers, the second request will carry the custom information. if("OPTIONS". Equals (Request.getmethod ())) {//here by judging the method of the request, whether this is a preflight request, if it is, immediately return a 204 status, marked, allow cross-domain; pre-check, formal request, this method parameter is our set of postResponse.setstatus (204);//httpstatus.sc_no_content = 204Response.setheader ("Access-control-allow-methods", "POST, GET, delete, OPTIONS, delete");//when a pre-test request is determined, the method that allows the request is setResponse.setheader ("Access-control-allow-headers", "Content-type, X-requested-with, Token");//sets the type of header allowed for a request after it is determined to be a preflight requestResponse.AddHeader ("Access-control-max-age", "3600"); } filterchain.dofilter (ServletRequest, servletresponse); } @Override Public voidInit (Filterconfig arg0)throwsservletexception { } }
This solves the problem of cross-domain, which can also be solved by using JSONP and configuring Nginx.
cors resolves Ajax cross-domain