Angular 通过'访问XMLHttpRequest;http://localhost:1111/...' 来自原点';http://localhost:4200' 已被CORS策略阻止:
由于CORS政策的阻碍,未发生交叉源代码 我使用的是Windows系统 在Angular 通过'访问XMLHttpRequest;http://localhost:1111/...' 来自原点';http://localhost:4200' 已被CORS策略阻止:,angular,cors,angular7,Angular,Cors,Angular7,由于CORS政策的阻碍,未发生交叉源代码 我使用的是Windows系统 在-.service.ts文件中: makeHeaders() { const headers = new HttpHeaders({ 'content':"application/json", 'Content-Type': 'application/x-www-form-urlencoded', // 'Access-Control-Allow-Credentials': '
-.service.ts
文件中:
makeHeaders() {
const headers = new HttpHeaders({
'content':"application/json",
'Content-Type': 'application/x-www-form-urlencoded',
// 'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Origin': 'http://localhost:4200',
'Access-Control-Allow-Methods': 'OPTIONS, GET, POST',
'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Access-Control-Allow-Origin, Authorization, X-Requested-With'
})
return headers;
}
"target": "http://localhost:1111"
在proxy.conf.json
文件中:
makeHeaders() {
const headers = new HttpHeaders({
'content':"application/json",
'Content-Type': 'application/x-www-form-urlencoded',
// 'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Origin': 'http://localhost:4200',
'Access-Control-Allow-Methods': 'OPTIONS, GET, POST',
'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Access-Control-Allow-Origin, Authorization, X-Requested-With'
})
return headers;
}
"target": "http://localhost:1111"
我在浏览器控制台中遇到此错误:
CORS策略已阻止从源“”访问“”处的XMLHttpRequest:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许源”标头。
core.js:15724错误HttpErrorResponse{headers:HttpHeaders,状态:0,状态文本:“未知错误”,url:,ok:false,…}
谢谢你可以试试这个
app.options("/*", function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.sendStatus(200);
});
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
});
在使用localhost时,需要安装插件
铬
火狐
谢谢仍然得到相同的错误。CORS策略已阻止从源“”访问“”处的XMLHttpRequest:请求的资源上不存在“访问控制允许源”标头。可以为本地开发或测试解决您的问题。cors问题无法从客户端应用程序修复。我可以看到您正在使用代理,但未正确设置。请检查: