JS cross-domain access to resources

Source: Internet
Author: User

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&gt: 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.