Search code examples
javascriptangularjsresthadoopwebhdfs

Enabling Cross domain Access in WEBHDFS REST Api to be used by JavaScript?


I am trying to GET liststatus from the Webhdfs rest api, but getting the following error.

XMLHttpRequest cannot load http://<IP>:50070/webhdfs/v1/?op=LISTSTATUS. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

It loads successfully when I try to access through curl. But it fails when I try to get it using AngularJS $http.get.

Is there any way to enable Cross domain access in Hadoop core-site.xml or somewhere else?


Solution

  • If I understood it right you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request. A tutorial about how to achieve that is Using CORS.

    When you are using postman they are not restricted by this policy. Quoted from Cross-Origin XMLHttpRequest:

    Check it in Firefox browser it may work or else ..

    I believe this might likely be that Chrome does not support localhost to go through the Access-Control-Allow-Origin -- see Chrome issue

    To have Chrome send Access-Control-Allow-Origin in the header, just alias your localhost in your /etc/hosts file to some other domain, like:

    127.0.0.1 localhost yourdomain.com Then if you'd access your script using yourdomain.com instead of localhost, the call should succeed. (or) That's problem of the server. You have to setup response headers on server side.