JS access resources By default using the same-origin policy
Same origin: domain name, same port
LocalHost and IP address are not homologous
Ajax cross-domain strict: Sub-domain name, the same domain name and port are different cross-domain
Firefox Ajax cross-domain does not automatically bring cookies
For cross-domain settings (multiple scenarios):
First, the container level, the impact range is under the container all WebApp applications in Tomcat/conf/web.xml ex:<filter> <filter-name> Corsfilter</filter-name> <filter-class>org.apache.catalina.filters.CorsFilter</filter-class> </filter><filter-mapping> <filter-name>CorsFilter</filter-name> <url-pattern>/* </url-pattern></filter-mapping>: This filter is for apache-tomcat-7.0.41 and above only. Second, a single application, only acting on the project itself in Webapp/web-inf/web.xml<filter> <filter-name>CorsFilter</filter-name> < Filter-class>org.apache.catalina.filters.corsfilter</filter-class></filter><filter-mapping > <filter-name>CorsFilter</filter-name> <url-pattern>/*</url-pattern></ Filter-mapping> three, specify all request resources for filter filtering
public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {
HttpServletRequest req = (httpservletrequest) request;
HttpServletResponse res = (httpservletresponse) response;
Res.addheader ("Access-control-allow-origin", "*");
Res.addheader ("Access-control-allow-methods", "GET, POST, OPTIONS");
Res.addheader ("Access-control-allow-headers", "Origin, No-cache, X-requested-with, If-modified-since, Pragma, Last-modified, Cache-control, Expires, Content-type, X-e4m-with ");
Chain.dofilter (req, res);
}
Four
Maven-archetype-wepapp offers two types of support tomcat7-maven-plugin<plugin> <groupId> org.apache.tomcat.maven</groupid> <artifactid>tomcat7-maven-plugin</artifactid > <version>2.2</version> <configuration> &NBS P <path>/servletdemo</path> <port>8082</port> <server>tomcat</server> < url>http://localhost:8080/manager/text</url> <!--Enable CORS-to-&N Bsp <tomcatWebXml>src/test/resources/tomcat.web.xml</tomcatWebXml> </configuration></plugin>jetty-maven-plugin<plugin> < Groupid>org.eclipse.jetty</groupid> <artifactId>jetty-maven-plugin</artifactId> & nbsp <version>9.3.2.v20150730</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> &NBSP ; <webApp> &NBSP ; <contextPath>/servletdemo</contextPath> &NB Sp <!--Fix file locking problem with Jettyrun e Nable cors--> <defaults descriptor>src/test/resources/jetty.web.xml</defaultsdescriptor> & nbsp </webApp>
JS cross-domain access to resources