1

I am facing "Access Control Origin not Allowed" issue in AngularJS and based on AngularJS CORS Issues I have added following lines in my code:

angular.module('app', [
    'ngRoute',
    'appModules.filters',
    'appModules.ajaxCallService'
])
.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {

    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];

    $routeProvider.when('/login', {templateUrl: 'views/login.html', controller: 'LoginCtrl'});
}]);

But still I am facing issue:

XMLHttpRequest cannot load http://127.0.0.1:8000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

Can anyone help me to solve this issue?

Even using localhost in URL I am getting same issue

XMLHttpRequest cannot load http://localhost:8000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

I am using AngularJS version 1.2.16 and expressJS with 0.10.26 nodejs.

Community
  • 1
  • 1
Sachin
  • 2,321
  • 11
  • 31
  • 49
  • apply same origin policy. check http://stackoverflow.com/questions/1131210/work-around-for-the-same-origin-policy-problem – user3470953 May 05 '14 at 10:03
  • Use http://127.0.0.1:8000 to test the app instead of http://localhost:8000. – JJJ May 05 '14 at 10:04
  • @Juhana : Even using localhost in URL i am getting same issue. – Sachin May 05 '14 at 10:09
  • You are on `http://localhost` (port 80) trying to access `http://localhost:8000`. Are you running two servers? – JJJ May 05 '14 at 10:10
  • No i am using expressjs and using 8000 port. – Sachin May 05 '14 at 10:14
  • @SachinKumbharkar — You *must* be running something on port 80, otherwise the error message wouldn't mention `http://localhost/`, it is where you are loading the HTML document from. – Quentin May 05 '14 at 10:36
  • You can't solve this problem on the client. You have to deal with it in Express. The Angular forums say that [`$httpProvider.defaults.useXDomain` is nonsense](https://groups.google.com/forum/#!topic/angular/kl2BVOubG4I), if it doesn't anything at all, it depends on the server recognizing whatever it does and using that to conditionally enable CORS support (which is silly, there isn't any sense is making CORS conditional on something that the client side code can specify). – Quentin May 05 '14 at 10:38
  • @Quentin: Yes i am using apache on 80 and nodejs on 8000. – Sachin May 05 '14 at 10:39

1 Answers1

4

My problem got resolved i have added

  res.setHeader('Access-Control-Allow-Origin', 'http://localhost');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
  res.setHeader('Access-Control-Allow-Credentials', true);

in my expressJS code while sending data to client applicatin.

Sachin
  • 2,321
  • 11
  • 31
  • 49