1

I am Newbie to angular js. Here I am trying to call API from rails server with "http://localhost:3000/". I got error for

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'

I have added 'CORS' chrome extension to fix it. Now the problem is my request is passing to localhost:3000 as [OPTIONS], and I want it as get request.

'use strict';

angular.module('dayton')
.service('foodCategoryService', ['$http', '$window', 'authService', 'Upload',
  function($http, $window, authService, Upload){
        var field = {};

      field.getFields = function (){
        console.log("---------foodCategoryService")
        return $http.get('http://localhost:3000/api/list_food_category',  {
          headers: {Authorization: authService.getToken()}
        })
        .then(function(response) {
          return response;
        }, function(x) {
          $scope.authError = 'Server Error';
        });
      };
        return field;
  }]);

ActionController::RoutingError (No route matches [OPTIONS] "/api/list_food_category"):

Any help is going to be appreciated

5
  • CORS plugin only can't fix it. Your server needs to allow request for CORS. Commented Dec 15, 2016 at 6:25
  • You don't need a CORS extension, if you are making calls and hosting your frontend on the same host, ie localhost right? Commented Dec 15, 2016 at 6:25
  • No, I am using two different servers here. localhost:8081 is my current server and I am trying to send api call to localhost:3000 Commented Dec 15, 2016 at 6:27
  • @Ved I have tried to add allow request for CORS. would you suggest me any link to refer ? I have tried with app.use(function (req, res, next) { .... but getting error for app and use function in express.js Commented Dec 15, 2016 at 6:31
  • 1
    check here: stackoverflow.com/questions/7067966/how-to-allow-cors Commented Dec 15, 2016 at 6:35

2 Answers 2

0

modify your app config

angular.module('dayton').config(function($httpProvider){

    $httpProvider.defaults.headers.common = {};
    $httpProvider.defaults.headers.post = {};
    $httpProvider.defaults.headers.put = {};
    $httpProvider.defaults.headers.patch = {};
});
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please tell me, In while file do I need to add this ?
in your controller file
I have added this lines in foodCategoryController.js . But same issue I got. request is passing as [OPTIONS] . not as get request.
0

It is not your angular js side problem. You have to allow cross origin request at your server end

2 Comments

For server side, I am using node.js . Can you please suggest which changes I need to do for that?
developer.mozilla.org/en-US/docs/Web/HTTP/Ac cess_control_CORS example code : app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.