【发布时间】: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 这就是我一直在做的 - 它会显示我的站点目录中的所有文件。