Angular 我如何在角形9中绕过CORS
我收到以下错误:Angular 我如何在角形9中绕过CORS,angular,cors,Angular,Cors,我收到以下错误: Access to XMLHttpRequest at 'http://localhost:40000/api/summaryCount/failed-requests' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-
Access to XMLHttpRequest at 'http://localhost:40000/api/summaryCount/failed-requests' from origin
'http://localhost:3000' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
我创建了一个proxy.conf.json文件,其中包含以下内容:
{"/api/*": {
"target": "http://localhost:40000",
"secure": false
}
}
httpHeaders = new HttpHeaders().append('Access-Control-Allow-Origin', 'http://localhost:3000').....
我还修改了angular.json文件,包括
"proxyConfig": "proxy.conf.json"
关于“发球”:{“选项”{
最后,我修改了我的package.json,使“开始”选项现在变成:
"start": "ng serve --proxy-config proxy.conf.json",
在我创建HttpHeaders对象时,如果我添加以下内容:
{"/api/*": {
"target": "http://localhost:40000",
"secure": false
}
}
httpHeaders = new HttpHeaders().append('Access-Control-Allow-Origin', 'http://localhost:3000').....
这仍然没有什么区别
有人能告诉我我错过了什么吗。你必须启用
CORS
并将你的Angular应用程序URLhttp://localhost:3000
在您的后端服务器上,该服务器位于http://localhost:40000/api/...
无需在Angular应用程序端执行任何操作。您必须启用
CORS
并将您的Angular应用程序URL列入白名单http://localhost:3000
在您的后端服务器上,该服务器位于http://localhost:40000/api/...
无需在Angular应用程序端执行任何操作。在后端服务器中,您必须将访问控制允许源代码写为*,如果您阻止特定URL,则您也可以将URL写在那里。在后端服务器中,您必须将访问控制允许源代码写为*,如果您阻止特定URLn您也可以在那里写入URL。您是否设置了诸如“访问控制允许标头”、“访问控制允许方法”之类的标头?您使用的是什么后端?
.NET
或NodeJS
?我使用.NET作为后端?您是否设置了诸如“访问控制允许标头”、“访问控制允许方法”之类的标头?您使用的是什么后端.NET
或NodeJS
?我正在使用.NET作为我的后端我的后端服务器是用C#hasvar cors=new EnableCorsAttribute(ConfigurationManager.AppSettings[“corsallowdorigins”],“*”,“*””{SupportsCredentials=true};config.EnableCors(cors)
C#编写的我的后端服务器具有var cors=new EnableCorsAttribute(ConfigurationManager.AppSettings[“corsallowdorigins”],“*”,“*”){SupportsCredentials=true};config.EnableCors(cors);