Monday, August 15, 2016

Webservice calling from WCS and getting Error: No 'Access-Control-Allow-Origin' header is present on the requested resource.

We Start where you are. Use what you have. Do what you can.
Very true.. Some times u need a starting position which gives initiative.
You will never get perfect time or place to start with.
WCS this is the short name of tool on which i m working now a days.
Little bit tough but different tool actually not different tool as it is much similar like WordPress which is a cms content management system.
The first work i got is to implement Hermes webservices in PDP product display page.
Since yesterday i was working but didn't get any success as webservices is generally use another domain and u cant connect localhost to another domain webserver without using cross domain authentication or SSL certificate sharing.
So i tried first to install  SELF Signed SSL certificate on chrome browser as this certificate is browser specific.  But no luck as ip address of webserver is more secure.

Second i found there is a domain url of production from which we can call Hermes webservices. Luckly it works on postman rest client app on chrome. But not working this URL in my localhost wcs application. I messed up as i was running wcs app on the same chrome browser on which postman client was installed and postman rest client was able to call Hermes webservices but not my web application.

Third step i did search the reason then I got postman is the realman :)  because it uses CORS cross site authentication by default and localhost don't have this so next step was to install cross domain authorization in my code.
And in jsp page u can add this by adding request header parameters as shown below:

<% response.setHeader("Access-Control-Allow-Origin", "*"); %>
 But no luck as wcs app is not accepted this changes in jsp as wcs has its own framework design to implement corp authentication.
Now i had only one option to search crop extention or plugin for chrome browser and fortunately i found it with the same name CORS and i install it which solved the problem.
Thank god :P