dvdcastro / keystonejs-ng-skeleton

KeystoneJS + AngularJS Skeleton
MIT License
72 stars 14 forks source link

Getting error "No 'Access-Control-Allow-Origin' header is present on the requested resource"? #9

Open useralive003 opened 7 years ago

useralive003 commented 7 years ago

my server is running in localhost:3000 and i have created posts now i am trying to access that post data from angular file

i have written like this in controller

myApp.controller('TestCtrl', function ($scope,$http) {

$scope.check = function() { $http.get('http://192.168.1.6:3000/keystone/posts/5876221082b5f21e383abba2').success(function(data, response) { console.log(data);

});

} });

i am getting error like this

XMLHttpRequest cannot load http://192.168.1.6:3000/keystone/posts/5876221082b5f21e383abba2. Redirect from 'http://192.168.1.6:3000/keystone/posts/5876221082b5f21e383abba2' to 'http://192.168.1.6:3000/keystone/signin?from=/keystone/posts/5876221082b5f21e383abba2' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

help me out to solve this issue

dvdcastro commented 7 years ago

Sorry for the very very late response. You should use the angular $resourse factory for calling web services.

https://github.com/dvdcastro/keystonejs-ng-skeleton/blob/master/public/js/ang_bootm/services.js

And then like this:

https://github.com/dvdcastro/keystonejs-ng-skeleton/blob/master/public/js/ang_bootm/post/post.js

  Post.get({slug: $routeParams.slug}, function(post) {
    self.post = post;
  });

I think there might be some middleware for adding cors headers so that you don't get this error, but you shouldn't call services using the whole URL if you are hosting you angular code from within the keystone project.