50

I'm writting my webApp and I'm using AngularJS. In this app I have created a file called script.js and I report this code:

var modulo = angular.module('progetto', ['ngRoute']);

    // configure our routes
    modulo.config(function ($routeProvider, $httpProvider) {
        $routeProvider

            // route for the home page
            .when('/', {
                templateUrl: 'listaFilm.html',
                controller: 'listaController'
            })

            // route for the description page
            .when('/:phoneName', {
                templateUrl: 'description.html',
                controller: 'descriptionController'
            });


            $httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';

    });


    modulo.controller('listaController', function ($scope, $http) {
        $http.get('https://api.getevents.co/event?&lat=41.904196&lng=12.465974').success(function (data) {
            $scope.names = data;
            }).
            error(function (data, status) {
                $scope.names = "Request failed";
            });
    });

With this code I call API following RESTful principles. When I run the code i have this problem:

XMLHttpRequest cannot load https://api.getevents.co No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access.

Reading on the web I understood that I have a problem called CORS...I have tried several solutions proposed but I didn't resolve the problem.
How can I fix the problem?
What's the code that I must add for fix it?

Arun A S
  • 6,421
  • 4
  • 29
  • 43
Alessandro
  • 905
  • 3
  • 17
  • 32
  • `https://api.getevents.co` needs to serve CORS headers in its responses. Do you control (or can you otherwise configure) the server at `https://api.getevents.co`? The server needs to change its behavior. – apsillers Apr 09 '15 at 19:20
  • not all API's are accessible by ajax if they are not either CORS enabled or jsonp enabled. Check the API docs or use a proxy – charlietfl Apr 09 '15 at 19:24
  • 1
    I controll and this is the respose: Our API supports CORS (Cross Origin Resource Sharing), which means that it can be called straight from the browser using JavaScript, or more traditionally from the server using the back-end language of your choice. – Alessandro Apr 09 '15 at 19:48
  • I shoul set the $http header but I don't do it – Alessandro Apr 09 '15 at 19:49
  • This answer maybe useful: https://stackoverflow.com/a/58064366/7059557 – AmirReza-Farahlagha Sep 23 '19 at 14:13

2 Answers2

65

This is a server side issue. You don't need to add any headers in angular for cors. You need to add header on the server side:

Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *

First two answers here: How to enable CORS in AngularJs

Community
  • 1
  • 1
Kristjan Liiva
  • 9,069
  • 3
  • 25
  • 26
32

CORS is Cross Origin Resource Sharing, you get this error if you are trying to access from one domain to another domain.

Try using JSONP. In your case, JSONP should work fine because it only uses the GET method.

Try something like this:

var url = "https://api.getevents.co/event?&lat=41.904196&lng=12.465974";
$http({
    method: 'JSONP',
    url: url
}).
success(function(status) {
    //your code when success
}).
error(function(status) {
    //your code when fails
});
Kabb5
  • 3,760
  • 2
  • 33
  • 55
Ritt
  • 3,181
  • 3
  • 22
  • 51