从CORS策略阻止从源“ http:// localhost:4200”访问“ URL”处的XMLHttpRequest:

CompanyDroneFromSector7G

我以为我可以访问所有来源,但仍然出现此错误。

CORS策略已阻止从来源“ http:// localhost:4200”访问“ localhost:60248 / api / page / 1”处的XMLHttpRequest:仅协议方案支持跨来源请求:http,data,chrome,chrome -扩展名,https。

UPDATE在添加建议的代码更改后,错误已更改。现在如下:

通过CORS策略已阻止从来源“ http:// localhost:4200”访问“ http:// localhost:60248 / api / page / 1”处的XMLHttpRequest:对预检请求的响应未通过访问控制检查:重定向不允许进行预检请求。

在Startup.cs中,我有以下内容:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(name: "AllowAll",
                builder =>
                {
                    builder.AllowAnyOrigin()
                        .WithMethods("PUT", "DELETE", "GET");
                });
        });

在我的控制器中,我有:

[Route("api/[controller]")]
[ApiController]
public class PageController : ControllerBase
{
    ...

    [EnableCors("AllowAll")]
    [HttpGet("{id:int}")]
    public async Task<IActionResult> GetPageData(int id)
    {
       ...

这是调用端点的Angular服务:

export class PageContentService {
  constructor(private http: HttpClient) { }

  getContent(id: number): Observable<PageContent> {

    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json');
    const httpOptions = {
      headers: headers
    }

    return this.http.get<PageContent>(`http://localhost:60248/api/page/${id}`, httpOptions);
  }
}

我认为可以解决这个问题,但显然不会。

我还需要做什么?

松鼠杀手

我认为您还需要两件事:

  1. 就像vivek所说的那样,在您的Configure方法中添加.UseCors()到您的请求管道中。

  2. 添加.AllowAnyHeader()到策略构建器中的CORS-Config中,我认为Content-Type默认情况下-Header是不允许的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

CORS 策略阻止了对源“http://localhost:4200”的 XMLHttpRequest 的访问

CORS策略已阻止访问源为“ http:// localhost:4200”的“ http:// ....”处的XMLHttpRequest

CORS 策略已阻止从源“http://localhost:8080”访问“http://localhost:8092/URL”处的 XMLHttpRequest

從源“http://localhost:4200”訪問“localhost:8088/user/”的 XMLHttpRequest 已被 CORS 策略阻止

通过CORS策略已阻止从源“ null”访问“ http://github/..file.json”处的XMLHttpRequest:

CORS 策略(Spring Boot 和 Angular 7)阻止了从源“http://...”访问“https://...”处的 XMLHttpRequest

CORS 策略已阻止从源“localhost:3000”访问“...”处的 XMLHttpRequest

CORS策略已阻止从来源“ http:// localhost:8000”访问“ http:// localhost / api”处的XMLHttpRequest

從源“http://localhost:62521”訪問“localhost:3000/users”的 XMLHttpRequest 已被 CORS 策略阻止

Angular7中的CORS策略已阻止源'http:// localhost:4200'

Spring Boot:即使使用@ CrossOrigin,CORS策略也阻止了从源FRONTEND在BACK_END'处访问XMLHttpRequest

CORS 策略已阻止从源“http://localhost:3000”访问“https://localhost:7144/api/employees”

CORS 策略阻止了从源“http://localhost:3000”获取 $AWS_LAMBDA_SITE 的访问权限

Angular 9 和 .NET Core - 从源 localhost:4200 访问已被 CORS 策略阻止

对XMLHttpRequest的访问已被CORS策略阻止

从CORS策略阻止从源“空”访问“ https:// localhost:44395”的访问

仅由于配置页面中的路径,导致了codeigniter中的CORS策略错误阻止了源“ http:// localhost”:-重复的问题

春季启动应用返回“ CORS”策略已阻止对“ myURL”处的XMLHttpRequest的访问

从源“null”访问 Twitter_OAuth_Url(从“http://localhost:3000/api/twitter-login”重定向)的 XMLHttpRequest 已被阻止

从源对XMLHttpRequest的访问已被CORS策略阻止:请求的资源上不存在“ Access-Control-Allow-Origin”标头

CORS 策略已阻止在“来自源”的 XMLHttpRequest 访问:请求中不存在“Access-Control-Allow-Origin”标头

使用 Axios 在 ReactJS 中访问被 CORS 策略阻止的 XMLHttpRequest

如何解决CORS策略已阻止对XMLHttpRequest的访问?

Vue + Golang:从源头访问 [Apiurl] 处的 XMLHttpRequest 已被 CORS 策略阻止:不允许请求头字段授权

CORS策略阻止localhost

'http://localhost:3000' 已被 CORS 策略阻止:没有 'Access-Control-Allow-Origin'

对 XMLHttpRequest 的访问已被 cors 策略阻止:对预检请求的响应未通过访问控制检查

春天开机有棱有角。访问的XMLHttpRequest在的 'http://本地主机:8080 /' 从原点的 'http://本地主机:4200' 已经被封锁CORS政策

无法从离子应用程序连接到Signalr Hub:CORS策略已阻止访问XMLHttpRequest ...