Wednesday, 11 May 2016

CORS - Cross Domain-Resource Sharing , Camel Restlet


Hi guys, I have  come  with new one on CORS - HTTP Stuff

CORS means CROSS DOAMIN RESOURCE SHARING

If you develop REST Application and tried testing with Chrome REST or Firefox REST Plugin or addons , it would have worked. But When u try with HTML page using JavaScript it might return with HTP - 405 (Method Not Allowed)

In my case it is issue with Camel - RESTLET. I have the same issue as below :
INFO:         13:10:38        127.0.0.1        -        -        24366        OPTIONS        /ORDER/COMPUTER        -        405        487        0        2        http://127.0.0.1:24366        Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0        -

Solution:

Before going to issue you have to understand the CORS concept. As it name says , it is cross domain .

Your code is actually attempting to make a Cross-domain (CORS) request, not an ordinary POST.
That is: Modern browsers will only allow Ajax calls to services in the same domain as the HTML page.
Example: A page in http://www.server.com/index.html can only directly request services that are in http://www.server.com, like http://www.server.com/testservice/etc. If the service is in other domain, the browser won't make the direct call (as you'd expect). Instead, it will try to make a CORS request.
To put it shortly, to perform a CORS request, your browser:
  • Will first send an OPTION request to the target URL
  • And then only if the server response to that OPTION contains the adequate headers (Access-Control-Allow-Origin is one of them) to allow the CORS request, the browse will perform the call (almost exactly the way it would if the HTML page was at the same domain).
    • If the expected headers don't come, the browser simply gives up (like it did to you).


I enabled the CORS for RESTLET in camel.
And also upgraded to Camel 2.17.1, which works well with CORS.Bcoz it doesn't support in earlier version which is less than Camel 2.16.2 .

Reference:

No comments:

Post a Comment

Please comment here