【问题标题】:Apache2 No 'Access-Control-Allow-Origin' header Despite proper tagsApache2没有'Access-Control-Allow-Origin'标头尽管有适当的标签
【发布时间】:2020-02-11 10:50:20
【问题描述】:

我在 apache2 localhost 服务器上使用 jQuery 检索 RSS 提要时遇到了一些问题。我正在运行 Mac OS X Catalina。当我运行这个 javascript:

$.get('url', function (data) {
    $(data).find("entry").each(function () { // or "item" or whatever suits your feed
        var el = $(this);

        console.log("------------------------");
        console.log("title      : " + el.find("title").text());
        console.log("author     : " + el.find("author").text());
        console.log("description: " + el.find("description").text());
    });
});

Chrome 给我以下错误日志:

从源“http://localhost”访问“url”处的 XMLHttpRequest 被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头 存在于请求的资源上。

我的/apache2/users/*.conf 看起来像这样:

DocumentRoot "/Users/*/Sites/"
<Directory "/Users/*/Sites/">
  Options FollowSymLinks Multiviews Indexes
  AllowOverride All
  MultiviewsMatch Any
  Require all granted

  Header add Access-Control-Allow-Origin "*"
  Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
  Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
  Header add Access-Control-Allow-Credentials "true"
  Header always set Access-Control-Allow-Origin "*"
</Directory>

我还确保安装了 mod_headers 并重新启动了 apache:

$ httpd -M |grep header
$ sudo apachectl restart

我仍然被抛出同样的错误。我已经阅读了所有我能找到的帖子,但仍然没有找到解决方案。任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 响应的 HTTP 状态码是什么?您可以使用浏览器开发工具中的网络窗格进行检查。是 4xx 还是 5xx 错误而不是 200 OK 成功响应?
  • Chrome 在网络窗格中仅将错误报告为(失败)。我没有给出具体的代码。虽然默认不是 200 响应,因为所有其他成功的连接都是这样列出的
  • 如果您尝试直接在浏览器中打开localhost 会发生什么?
  • @sideshowbarker 这就是我一直在做的 - 它会显示我的站点目录中的所有文件。

标签: apache cors


【解决方案1】:

经过更多研究,我遇到了this 的答案,它建议使用Moesif's Orign & CORS Changer。有了这个插件,我可以从提要中得到响应。

【讨论】:

    猜你喜欢
    • 2016-01-21
    • 1970-01-01
    • 2017-09-19
    • 2015-04-02
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多