是的,我发现了很多类似的东西,但对于我的实例仍然无法解决。我想避免破坏浏览器的安全性,而是让代理来完成它的工作,但是我担心我会丢失设置中的一些无用的细节,而且我绝不是CORS专家。
所以,我得到的是... a 403;
Failed to load http://localhost:10000/some/rest/endpoint: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
其中localhost:10000
是我的api网址,而:4200
是默认的Angular CLI实例。
因此,我希望在angular.json中配置一个代理;
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "angular-proj:build",
"proxyConfig": "./proxy.conf.json"
},
并添加proxy.conf.json;
{
"/some/rest/*": {
"target": "http://localhost:10000",
"secure": false,
"changeOrigin": true,
"logLevel": "debug"
}
}
我将其提供,甚至标记CLI服务确认的proxy.conf;
[HPM] Proxy created: /some/rest -> http://localhost:10000
等...
....除了我仍然得到403,并且仍然在Request *标头中看到;
Host: localhost:10000
Origin: http://localhost:4200
所以我的问题是,我在这里缺少什么愚蠢的细节?我不想对所有来源的请求都加星号,也不想关闭浏览器检查,我更喜欢将其很好地捆绑在服务配置中,就像我这样做时一样眼睛比欢迎。干杯
附录:我的GET似乎运行正常,但是类似Access-Control-Request-Method: POST
的内容显示为Request Method: OPTIONS
,这就是我收到403的地方...为什么要发布POST一个选项?
附录2 :值得一提的是,我在Chrome / Firefox中遇到了这个问题,但是Edge中的一切都是洁净的??
附录3 :该服务器正在作为--aot=true
与代理一起使用。
答案 0 :(得分:3)
以下标头是错误的,这就是为什么您的浏览器仍触发“相同原始策略”握手的原因:
Host: localhost:10000
Origin: http://localhost:4200
它们应该是:
Host: localhost:4200
Origin: http://localhost:4200
我坚信您不会将您的请求从http://localhost:10000/*
更改为http://localhost:4200/some/rest/*
,并且“大量相似的内容”不包含以下内容:Angular2 CORS issue on localhost
答案 1 :(得分:3)
问题是通过挖掘更多而发现的。搞清楚在服务器端(aspnet api v1)的链接配置中有一个隐藏的条件,供OPTIONS查找UrlReferrer.Authority
,而该条件在新的首次实例中不存在。
将其抽出,相应地更改Access-Control-Allow-Origin
,然后开始比赛。
欢呼