Ajax cross-domain access (from Tomcat8 to Apache)

Source: Internet
Author: User

1. Put the following files in the root directory of Tomcat

Apache-tomcat-8.0.12x64\webapps\root

ClientAccessPolicy.xml file

<?XML version= "1.0" encoding= "Utf-8"?><Access-policy>  <cross-domain-access>    <Policy>      <Allow-fromhttp-request-headers="*">        <DomainURI="*"/>      </Allow-from>      <grant-to>        <ResourcePath="/"include-subpaths= "true"/>      </grant-to>    </Policy>  </cross-domain-access></Access-policy>

Crossdomain.xml

<? XML version= "1.0" ?>    < Cross-domain-policy >    <  domain= "*"/></cross-domain-policy  >

2. Add the following code to the project's Web. xml

Apache-tomcat-8.0.12x64\webapps\test\web-inf

Xml

<?XML version= "1.0" encoding= "Iso-8859-1"?><Web-appxmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4">  <Display-name></Display-name>  <welcome-file-list>    <Welcome-file>Index.html</Welcome-file>  </welcome-file-list>    <Filter>      <Filter-name>Corsfilter</Filter-name>      <Filter-class>Org.apache.catalina.filters.CorsFilter</Filter-class>      <Init-param>        <Param-name>Cors.allowed.origins</Param-name>        <Param-value>*</Param-value>      </Init-param>      <Init-param>        <Param-name>Cors.allowed.methods</Param-name>        <Param-value>Get,post,head,options,put</Param-value>      </Init-param>      <Init-param>        <Param-name>Cors.allowed.headers</Param-name>        <Param-value>Content-type,x-requested-with,accept,origin,access-control-request-method,access-control-request-headers</Param-value>      </Init-param>      <Init-param>        <Param-name>Cors.exposed.headers</Param-name>        <Param-value>Access-control-allow-origin,access-control-allow-credentials</Param-value>      </Init-param>      <Init-param>        <Param-name>Cors.support.credentials</Param-name>        <Param-value>True</Param-value>      </Init-param>      <Init-param>        <Param-name>Cors.preflight.maxage</Param-name>        <Param-value>10</Param-value>      </Init-param>    </Filter>    <filter-mapping>      <Filter-name>Corsfilter</Filter-name>      <Url-pattern>/*</Url-pattern>    </filter-mapping>    <!--<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> -</Web-app>

3. In Apache httpd.conf, uncomment the LoadModule headers_module modules/mod_headers.so earlier

Apache\conf

4. Modify the Apache Httpd.conf <Directory> config section

Apache\conf

<directory "d:/project/resource/" >
AllowOverride None
Require all granted
Header Set Access-control-allow-origin *
Header set access-control-allow-headers "*"
Header set Access-control-allow-methods "POST, GET, OPTIONS, DELETE, PUT"
</Directory>

--------------------------------------------------------------------------------------------------------------- ---------

Ajax can be used to access data from Apache in the Tomcat Project's HTML page

var url = http://127.0.0.1/resource/JavaScript/gis/Z_QPF_20140831000000.F060.bin.bz2;
functionLoadData () {varXHR =NewXMLHttpRequest (); if("Withcredentials"inchxhr) {Xhr.open ("Get", url,true); Console.log ("Withcredentials"); } Else if(typeofXdomainrequest! = "undefined") {XHR=Newxdomainrequest (); Xhr.open ("Get", URL); Console.log ("Xdomainrequest"); } Else{XHR=NULL; } Xhr.responsetype= "Arraybuffer"; Xhr.onload=function() {        if( This. Status = = 200) {            varArrayBuffer = This. Response; //......        }    }}

Ajax cross-domain access (from Tomcat8 to Apache)

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.