【问题标题】:still having cors issue after adding Access-Control-Allow-Origin header in apache2在 apache2 中添加 Access-Control-Allow-Origin 标头后仍然存在 cors 问题
【发布时间】:2018-10-12 09:34:54
【问题描述】:

我在我的 apache2.conf 文件中添加了以下行来解决 cors 问题。

<Directory /var/www/html/rcycLatest2/RCYC/presentation/angular/rcyc>  
Header set Access-Control-Allow-Origin "http://localhost:4200" 
</Directory> 

但它仍然显示以下错误

Failed to load http://localhost/angular5/lesson2.pdf: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

我也试过用 '*' 代替 Access-Control-Allow-Origin url,但它仍然显示同样的错误。我已经在这个问题上花了一天时间,有人可以帮我解决这个问题。

【问题讨论】:

  • 之后您是否尝试重新启动 apache?,确保项目的配置和路径正确,您是否尝试在 chrome 或 firefox 的 Private Window 中以隐身模式访问它?有用信息:enable-cors.org/server_apache.html
  • 我点击此链接 [poanchen.github.io/blog/2016/11/20/… 启用 cors,并按照该页面中提供的指南完成所有操作。
  • 试图以隐身模式访问它,但仍然显示相同的错误。
  • 链接失效,apache 有多个配置文件,确保你添加到正确的文件中
  • 我在 apache2.conf 文件中添加了它。

标签: apache2


【解决方案1】:

我可以建议 2 个选项:

选项1:

如果您的后端服务器是节点(或者如果您刚刚开始一个项目并且不介意快速安装node):

我建议您使用Express.js,这将是您本地最快的 REST API 设置,您会看到我设置 cors 是多么容易:

let express = require('express');
let cors = require('cors');//<= CORS package install this with "npm i cors" in your console

let app = express();


 app.use(cors()); //<== this solves your cors issue

app.get('/', (req,res) => {
    console.log('ready and listening on port 3000');
    res.send('hello there, cors issue solved!);
});

app.listen(3000);

一旦你运行这个index.js,你的节点服务器将位于http://localhost:3000

选项 2:

另一种方法是使用Moesif Origin Extension,而 TBH 是一种更快的方法,这足以说明您需要 chrome。

神速。

【讨论】:

    猜你喜欢
    • 2017-12-11
    • 2021-10-20
    • 2018-03-04
    • 2016-10-30
    • 1970-01-01
    • 2019-06-17
    • 2017-11-04
    • 2016-05-07
    • 2016-10-30
    相关资源
    最近更新 更多