Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Post未能在飞行前阻止CORS策略,表示否';访问控制允许原点';标题,即使I';我在拦截器中添加了它_Angular_Asp.net Web Api_Cors - Fatal编程技术网

Angular Post未能在飞行前阻止CORS策略,表示否';访问控制允许原点';标题,即使I';我在拦截器中添加了它

Angular Post未能在飞行前阻止CORS策略,表示否';访问控制允许原点';标题,即使I';我在拦截器中添加了它,angular,asp.net-web-api,cors,Angular,Asp.net Web Api,Cors,使用:Angular 7、.net内核、web api 我最近将我的应用程序更改为使用windows身份验证,现在我曾经工作过的请求在任何post和put(get works)中都失败了。我得到一份工作 Access to XMLHttpRequest at 'https://localhost:44372/api/tasks' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to p

使用:Angular 7、.net内核、web api

我最近将我的应用程序更改为使用windows身份验证,现在我曾经工作过的请求在任何post和put(get works)中都失败了。我得到一份工作

Access to XMLHttpRequest at 'https://localhost:44372/api/tasks' from 
origin 'http://localhost:4200' 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.
我必须添加一个角度拦截器来获取get请求,以便在启用windows身份验证的情况下工作

@Injectable()
export class CredentialsInterceptor implements HttpInterceptor {

intercept(request: HttpRequest<any>, next: HttpHandler): 
Observable<HttpEvent<any>> {
    request = request.clone({
        withCredentials: true
    });
    return next.handle(request);
  }
}
我还尝试指定了起源或方法 .WithOrigins(“,”) 或 .AllowAnyOrigin()

以及在拦截器中手动添加标头

intercept(request: HttpRequest<any>, next: HttpHandler): 
Observable<HttpEvent<any>> {
    request = request.clone({
        withCredentials: true
    });
    request.headers.append('Access-Control-Allow-Origin', 'http://localhost:4200');
    return next.handle(request);
intercept(请求:HttpRequest,下一步:HttpHandler):
可观察{
request=request.clone({
证书:正确
});
request.headers.append('Access-Control-Allow-Origin','http://localhost:4200');
下一步返回。处理(请求);

我遗漏了什么?我已经在谷歌上搜索了几个小时。我知道如果没有指定允许的来源,CORS将被阻止。其他StackOverflow说它不能是客户端,但我觉得客户端请求没有发送“访问控制允许来源”头。我如何测试以确保该头已设置?为什么会得到工作而不是放置请求?

服务器必须在其响应中设置标头。在来自无用客户端的请求中设置标头。尝试使用postman之类的工具向api端点发出请求,并查看服务器在响应中设置的标头


很可能是服务器端代码中有问题导致了此问题。

所有其他帖子都说明了问题是什么,但没有说明问题的解决方案。选项请求是在没有授权标头的情况下提出的。我能够发出GET请求,因为windows身份验证标头是通过alo传递的ng但当我发出PUT请求时,必须首先发送不包含标头的选项


为了让服务器允许这些选项,我必须允许对这些选项进行匿名身份验证。我通过在“项目属性->调试”部分选择它来做到这一点。

响应的HTTP状态码是什么?您可以使用浏览器devtools中的“网络”窗格进行检查。这是4xx还是5xx错误,而不是200 OK成功响应?我这是401 unauthorizedYeah,因此401 unauthorized error是您需要解决的问题。有关CORS协议的详细说明,请参阅上的答案。要解决此问题,您需要更改服务器端代码以允许未经验证的选项请求。我可以从postman处发出请求。这只能从UI,由于corsSorry,它失败了,我没有降低你的分数,我感谢建议的帮助是的,你可以从邮递员那里提出请求。检查你的响应标题,看看是否设置了CORS标题。
intercept(request: HttpRequest<any>, next: HttpHandler): 
Observable<HttpEvent<any>> {
    request = request.clone({
        withCredentials: true
    });
    request.headers.append('Access-Control-Allow-Origin', 'http://localhost:4200');
    return next.handle(request);