Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

javascript - AngularJS:请求的资源上没有“Access-Control-Allow-Origin”标头[重复](AngularJS: No “Access-Control-Allow-Origin” header is present on the requested resource [duplicate])

I'm writting my webApp and I'm using AngularJS.(我正在写我的webApp,我正在使用AngularJS。)

In this app I have created a file called script.js and I report this code:(在这个应用程序中,我创建了一个名为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"; }); }); With this code I call API following RESTful principles.(使用此代码,我按照RESTful原则调用API。) 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.(XMLHttpRequest无法加载https://api.getevents.co请求的资源上没有“Access-Control-Allow-Origin”标头。) Origin ' http://localhost:8383 ' is therefore not allowed access.(因此,不允许Origin'http:// localhost:8383 '访问。) 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.(在网上阅读我明白我有一个叫CORS的问题...我已经尝试了几个解决方案,但我没有解决问题。)
How can I fix the problem?(我该如何解决这个问题?)
What's the code that I must add for fix it?(我必须添加什么代码来修复它?)   ask by Ragnarr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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(前两个答案: 如何在AngularJs中启用CORS)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...