【问题标题】:'Access-Control-Allow-Origin' issue“访问控制允许来源”问题
【发布时间】:2013-11-15 21:57:18
【问题描述】:

我有一个带导轨后端的角度前端。

我有这样的角度代码:

$http({method: 'GET', url: 'http://localhost:3000/products.json'}).
  success(function(data, status, headers, config) {
     $scope.data = data;
  }).
  error(function(data, status, headers, config) {
     $scope.status = status;
});

我一直收到No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access. 错误,所以我使用了https://stackoverflow.com/a/17815546/561634,所以我的 config/application.rb 有:

config.action_dispatch.default_headers = {
  'Access-Control-Allow-Origin' => '*',
  'Access-Control-Request-Method' => '*'
}

在里面。不幸的是,这并没有改变错误。

感谢大家的帮助!

【问题讨论】:

  • 你在哪个网站上设置的?如果site1 向site2 调用ajax 请求,你应该在site1 上设置它。对于这个烦人的问题,我放弃了设置该配置,我现在使用 jsonp。你也可以试试。

标签: javascript ruby-on-rails angularjs ruby-on-rails-4 access-control


【解决方案1】:

请确保您的后端没有会导致“500 - 内部服务器错误”的错误,因为在这种情况下可能不会发送“访问控制”标头。

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 2023-04-01
    • 2015-12-26
    • 2016-05-07
    • 1970-01-01
    • 2021-09-07
    • 2018-11-08
    相关资源
    最近更新 更多