Wednesday, December 28, 2016

How to get ride of access-control-allow-origin error

If you are getting access-control-allow-origin error while calling webservice using *.js (i.e. node.js/angular.js) follow below step to resolve it

Step 1:- access-control-allow-origin error means server need to send response with below value in response


Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: origin, x-requested-with, content-type
Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS

Make sure if you are calling web service add above parameter as header in response.


Step 2:- Make following changes in Tomcat on which your application is installed.

Add filer in web.xml of tomcat where your Angularjs or nodejs application resides.
<!--filter>
  <!--filter-name>header<!--/filter-name>
  <!--filter-class>com.thetransactioncompany.cors.CORSFilter<!--/filter-class>
<!--/filter>
<!--filter-mapping>
  <!--filter-name>header<!--/filter-name>
  <!--url-pattern>/*<!--/url-pattern>
<!--/filter-mapping>

Add this in Maven or if required copy this jar along with its dependend jar and place it in lib folder of Tomcat


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

No comments: