【问题标题】:No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问。
【发布时间】:2014-04-19 00:44:25
【问题描述】:

我在 Django 中创建了一个 API,并从桌面创建了一些用于 angular 和 html 的普通文件。我喜欢用 API 输出填充 Angular 应用程序。请求到达网络服务器,但随后出现错误。

我收到以下错误:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问。

所以我需要添加“Access-Control-Allow-Origin”= True 或其他内容,但是在哪里以及如何添加?我现在已经阅读了很多问题,尝试了不同的东西在哪里以及如何添加,但我无法弄清楚。有人可以帮我吗?提前谢谢。

views.py

def myjson(request):
    data = Dish.objects.all()
    #data = {'string':'test', 'twee':'pollo'}
    #data = serializers.serialize('json', data)
    data = json.dumps( [{'name': o.name, 'description': o.description, 'persons': o.persons} for o in data])
    return HttpResponse(json.dumps(data),mimetype="application/json")

script.js

var myApp = angular.module('myApp', []);

myApp.controller('UserCtrl', ['$scope', '$http', function ($scope, $http) {

  $scope.dish = {};

  //$scope.dish = [{"persons": 4, "name": "Pasta Bolognese", "description": "Pasta met een saus op tomaten basis"}, {"persons": 2, "name": "Pasta Carbonara", "description": "gdsdgfgds"}, {"persons": 4, "name": "Pizzadeeg voor Big Green Egg", "description": "sdgfgfdgfd"}, {"persons": 2, "name": "Low and Slow Ribs", "description": "fdsfsdsfdfsddf"}];

  //$scope.dish = '';

  $http({
    method: 'GET',
    url: 'http://127.0.0.1:8000/food/myjson/'
  })
  .success(function (data) {
    // See here, we are now assigning this username
    // to our existing model!
    $scope.dish = data;
  })
  .error(function (data, status, headers, config) {

  });
}]);

html

<!DOCTYPE html>
<html>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular-resource.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-route.js"></script>
<script src="script.js"></script>
<script type="text/javascript">
</script>

</head>

<body>
<div ng-app="myApp">
    <div ng-controller="UserCtrl">

                {{ dish }} 
    </div>
</div>
</body>

</html>

【问题讨论】:

    标签: django angularjs angularjs-scope


    【解决方案1】:

    这是一个CORS (cross-origin resource sharing) 问题。当您尝试访问不同域上的端点时会发生这种情况。因此,如果您的后端有 REST API,那将是有意义的。

    您的 API 需要设置标头以允许来自各种来源的请求。我们必须允许 Access-Control-Allow-Origin 允许 localhost 用于我们的开发环境。我发现在 Angular 中,对于每个请求,您还需要发送 withCredentials: true。

      $http({withCredentials: true, method: 'GET', url: http://127.0.0.1:8000/food/myjson/' })
      .success(function (data) {
          // See here, we are now assigning this username
          // to our existing model!
          $scope.dish = data;
      })
      .error(function (data, status, headers, config) {
    
      });
    

    });

    一些消息来源说,您还需要在客户端为每个请求设置标头。不过,我不必这样做。但包含它可能不会有什么坏处:

    headers: {'Content-Type': 'application/json; charset=utf-8'}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-09
    • 2014-03-13
    • 2016-05-27
    • 2018-07-06
    • 2013-12-24
    • 2018-10-23
    • 2017-07-17
    • 2017-02-04
    相关资源
    最近更新 更多