AngularJS:否];访问控制允许来源"头是在被请求的资源present [英] AngularJS: No "Access-Control-Allow-Origin" header is present on the requested resource

查看:2455
本文介绍了AngularJS:否];访问控制允许来源"头是在被请求的资源present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我书面方式我的webapp,我用AngularJS。在这个应用程序,我创建了一个文件调用的script.js,我这个报告code:

  VAR模= angular.module('PROGETTO',['ngRoute']);    //配置我们的路线
    modulo.config(函数($ routeProvider,$ httpProvider){
        $ routeProvider            //为主页路线
            。什么时候('/', {
                templateUrl:listaFilm.html',
                控制器:'listaController
            })            //用于描述网页路线
            。当('/:phoneName',{
                templateUrl:description.html',
                控制器:'descriptionController
            });
            $ httpProvider.defaults.headers.common ['访问控制允许来源'] ='*';    });
    modulo.controller('listaController',函数($范围,$ HTTP){
        $ http.get('https://api.getevents.co/event?&lat=41.904196&lng=12.465974').success(功能(数据){
            $ scope.names =数据;
            })。
            错误(功能(数据,状态){
                $ scope.names =请求失败;
            });
    });

有了这个code我称之为API以下RESTful的原则。当我运行code,我有这样的问题:


  

XMLHtt prequest无法加载 https://api.getevents.co
  访问控制允许来源标头present的要求
  资源。原产地的http://本地主机:8383 因此不允许
  访问。


阅读我明白,我有一个问题叫做CORS在网络上...我曾尝试提出了几种解决方案,但我并没有解决问题。结果
我该如何解决这一问题?结果
什么是code,我必须增加对解决这个问题?


解决方案

这是一个服务器端的问题。你不需要在角添加任何标头CORS。
您需要在服务器端添加标题:

 接入控制允许头:内容类型
访问控制允许的方法:GET,POST,OPTIONS
访问控制允许来源:*

在这里首先两个答案:如何启用AngularJs

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?

解决方案

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:否];访问控制允许来源"头是在被请求的资源present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆