我无法 运行 angular 申请
I am not able to run angular application
错误:从来源 'http://localhost:4200' 访问 'localhost:8080/api/products' 处的 XMLHttpRequest 已被 CORS 策略阻止:跨来源请求仅支持协议方案:http、数据、chrome、 chrome-扩展名,https。
:8080/api/products:1 加载资源失败:net::ERR_FAILED
即使在 server.js
中添加这些之后
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods","GET,POST,PUT,DELETE");
next();
});
客户端的问题可能是由于浏览器选择处理 pre-flight CORS 的行为造成的。使用 OPTIONS 方法而不是使用 GET 方法。建议添加用于处理 OPTIONS 的处理程序。这是代码:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Origin, Content-Type, Accept, Accept-Language, Origin, User-Agent');
if(req.method === 'OPTIONS') {
res.sendStatus(204);
} else {
next();
}
});
如果您正在使用 Angular CLI,您可能需要通过以下配置在 angular 应用程序根文件夹中添加 proxy.conf.json 文件来启用代理:
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}
虽然 运行 Angular 应用程序包含 --proxy-config 如下或更适当地编辑 pacakage.json 启动脚本以包含配置:
ng serve --proxy-config proxy.conf.json
上的好文章
解决方案 1:只需使用 app.use(cors()) .
解决方案 2:app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // 更新以匹配您将从中发出请求的域
res.header("Access-Control-Allow-Headers", "来源, X-Requested-With, Content-Type, 接受");
res.header("Access-Control-Allow-Headers","GET,POST,PUT,DELETE");
下一个();
});
错误:从来源 'http://localhost:4200' 访问 'localhost:8080/api/products' 处的 XMLHttpRequest 已被 CORS 策略阻止:跨来源请求仅支持协议方案:http、数据、chrome、 chrome-扩展名,https。 :8080/api/products:1 加载资源失败:net::ERR_FAILED
即使在 server.js
中添加这些之后app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods","GET,POST,PUT,DELETE");
next();
});
客户端的问题可能是由于浏览器选择处理 pre-flight CORS 的行为造成的。使用 OPTIONS 方法而不是使用 GET 方法。建议添加用于处理 OPTIONS 的处理程序。这是代码:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Origin, Content-Type, Accept, Accept-Language, Origin, User-Agent');
if(req.method === 'OPTIONS') {
res.sendStatus(204);
} else {
next();
}
});
如果您正在使用 Angular CLI,您可能需要通过以下配置在 angular 应用程序根文件夹中添加 proxy.conf.json 文件来启用代理:
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}
虽然 运行 Angular 应用程序包含 --proxy-config 如下或更适当地编辑 pacakage.json 启动脚本以包含配置:
ng serve --proxy-config proxy.conf.json
上的好文章
解决方案 1:只需使用 app.use(cors()) .
解决方案 2:app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); // 更新以匹配您将从中发出请求的域 res.header("Access-Control-Allow-Headers", "来源, X-Requested-With, Content-Type, 接受"); res.header("Access-Control-Allow-Headers","GET,POST,PUT,DELETE"); 下一个(); });