No 'Access-Control-Allow-Origin' header is present on the requested resource

  Jul 22, 2015

I am trying to send a json get request from localhost to live server but getting following error 

No 'Access-Control-Allow-Origin' header is present on the requested resource

Please help

 

Solved 0
2 years ago 1 Answer 22 views

Answers ( 1 )

  1. 2 years ago

    After a rigorous research i found out the solution. May be helpful to someone. 
    here is the solution. 

    2 Steps: 

    1) added a dependency in pom.xml 

    <dependency>
                <groupId>com.thetransactioncompany</groupId>
                <artifactId>cors-filter</artifactId>
                <version>1.7</version>
            </dependency>


    2) Added a filer mapping in the web.xml 

    <filter>
            <filter-name>CORS</filter-name>
            <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
            <init-param>
                <param-name>cors.allowOrigin</param-name>
                <param-value>*</param-value>
            </init-param>
            <init-param>
                <param-name>cors.supportedMethods</param-name>
                <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
            </init-param>
            <init-param>
                <param-name>cors.supportedHeaders</param-name>
                <param-value>Accept, Origin, X-Requested-With, Content-Type,
                    Last-Modified</param-value>
            </init-param>
            <init-param>
                <param-name>cors.exposedHeaders</param-name>
                <param-value>Set-Cookie</param-value>
            </init-param>
            <init-param>
                <param-name>cors.supportsCredentials</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
     
        <filter-mapping>
            <filter-name>CORS</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>


    And thats it. it all worked for me. 
    Cheers guys.

Your Answer