简体   繁体   English

ASP.NET CORE 中的 CORS 策略已阻止访问 XMLHttpRequest

[英]Access to XMLHttpRequest has been blocked by CORS policy in ASP.NET CORE

I tried to make connection from my side client to the API Server with ajax, but I get a error in the response: Access to XMLHttpRequest at ' https://localhost:44381/api/webinars ' from origin ' http://localhost:5003 ' 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. I tried to make connection from my side client to the API Server with ajax, but I get a error in the response: Access to XMLHttpRequest at ' https://localhost:44381/api/webinars ' from origin ' http://localhost :5003 '已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”header。 I tried to edit the CROS policy in the API but I still have the same error.我试图在 API 中编辑 CROS 策略,但我仍然有同样的错误。 This is my ajax code from MVC:这是我来自 MVC 的 ajax 代码:

 $.ajax({
            url: "https://localhost:44381/api/webinars",
            type: 'POST',
            headers: {
                contentType: "application/json",
            },
            body: JSON.stringify(body),
            success: function (data) {
                console.log(data);
            },
            failure: function (err) {
                console.log(err);
            }

        });

StartUp from API:从 API 启动:

  public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddCors(options =>
        {
            options.AddPolicy("AllowMyOrigin",
            builder => builder.WithOrigins(
                "http://localhost:5003/",
                "http://apirequest.io")
                .WithMethods("POST","GET","PUT")
                .WithHeaders("*")
                );
        });
          //code
    }

   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //code
        app.UseCors("AllowMyOrigin");
        app.UseHttpsRedirection();
        app.UseMvc();
    }
}

Controller in the API: API 中的 Controller:

[EnableCors("AllowMyOrigin")]

[Route("api/[controller]")]
[ApiController]
public class WebinarsController : ControllerBase
{
        // POST: api/Webinars
    [HttpPost]
    public async Task <ActionResult> PostAsync([FromBody] Item newItem)
    {     
         //code
      }
 }

Thanks for the answer.感谢你的回答。

you should replace app.UseMvc();你应该替换 app.UseMvc(); with

app.UseEndpoints(endpoints =>
{
   endpoints.MapControllers();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决CORS策略已阻止对XMLHttpRequest的访问? - How to solve Access to XMLHttpRequest has been blocked by CORS policy? 从源访问 xmlhttprequest 已被 Angular 6 中的 cors 策略阻止 - Access to xmlhttprequest at from origin has been blocked by cors policy in Angular 6 从CORS策略阻止从原始位置访问“ http:// localhost…”处的XMLHttpRequest: - Access to XMLHttpRequest at 'http://localhost…' from origin has been blocked by CORS policy: 从源“http://localhost:4200”访问 XMLHttpRequest 已被 CORS 策略(Angular,WebApi)阻止 - Access to XMLHttpRequest from origin 'http://localhost:4200' has been blocked by CORS policy (Angular, WebApi) CORS 策略已阻止从源“http://localhost:4200”访问“URL”处的 XMLHttpRequest: - Access to XMLHttpRequest at 'URL' from origin 'http://localhost:4200' has been blocked by CORS policy: CORS 策略已阻止从原点 '' 在 '' 访问 XMLHttpRequest:没有 'Access-Control-Allow-Origin' header 存在于请求的 - Access to XMLHttpRequest at '' from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested 请求始终被CORS策略C#Net Core阻止 - Request always has been blocked by CORS policy c# net core CORS 策略已阻止从源“http://**”访问“https://saja.smjd.ir/api/Account/login”处的 XMLHttpRequest - Access to XMLHttpRequest at 'https://saja.smjd.ir/api/Account/login' from origin 'http://**' has been blocked by CORS policy CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost:25776/api/Upload”处的 XMLHttpRequest - Access to XMLHttpRequest at 'http://localhost:25776/api/Upload' from origin 'http://localhost:4200' has been blocked by CORS policy CORS 策略 .net 6 已阻止从源“LOCALHOST”访问“API URL”获取 - Access to fetch at 'API URL' from origin 'LOCALHOST' has been blocked by CORS policy .net 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM