[Answered ]-No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

2👍

This is a CORS (cross-origin resource sharing) issue. It happens when you try to hit an endpoint that is on a different domain. So that’d make sense if you have a REST API for your backend.

Your API needs to set headers to allow requests from various sources. We had to allow Access-Control-Allow-Origin to allow localhost for our dev environments. I found that in Angular, with each request, you need to send withCredentials: true as well.

  $http({withCredentials: true, method: 'GET', url: http://127.0.0.1:8000/food/myjson/' })
  .success(function (data) {
      // See here, we are now assigning this username
      // to our existing model!
      $scope.dish = data;
  })
  .error(function (data, status, headers, config) {

  });

});

Some sources say you also need to set the header on the client side as well for each request. I haven’t had to do that though. But it may not hurt to include it:

headers: {'Content-Type': 'application/json; charset=utf-8'}

Leave a comment