<分区>
我正在编写我的 webApp,我正在使用 AngularJS。在这个应用程序中,我创建了一个名为 script.js 的文件,我报告了这段代码:
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";
});
});
使用此代码,我按照 RESTful 原则调用 API。当我运行代码时,我遇到了这个问题:
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.
在网上阅读后我了解到我遇到了一个名为 CORS 的问题...我已经尝试了几种提出的解决方案,但我没有解决问题。
我该如何解决这个问题?
我必须添加什么代码才能修复它?