第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在啟動中啟用 CORS 失敗并出現預檢錯誤

在啟動中啟用 CORS 失敗并出現預檢錯誤

C#
慕森卡 2022-01-16 15:09:52
我正在嘗試在 Startup.cs 中啟用 CORS,但沒有成功。我在端口 4200 上有一個 Angular 應用程序,試圖與我的 c# Web 應用程序通信。我在 Angular 中不斷收到此錯誤無法加載http://localhost:52008/Account/GetJWT:預檢響應沒有 HTTP ok 狀態(tài)。我的研究似乎表明 CORS 未正確啟用。知道我錯過了什么嗎?public void ConfigureServices(IServiceCollection services){.....    services.AddCors(options =>    {        options.AddPolicy("EnableCORS", builder =>        {            builder.AllowAnyOrigin()            .AllowAnyMethod()            .AllowAnyHeader()            .AllowCredentials()            .Build();        });    });    .........}public void Configure(IApplicationBuilder app, IHostingEnvironment env){....... app.UseCors("EnableCORS"); ........ }這是 Angular POST 請求:  login(form: NgForm) {    let credentials = JSON.stringify(form.value);    console.log(credentials);    this.http.post("http://localhost:52008/Account/GetJWT", credentials, {      headers: new HttpHeaders({        "Content-Type": "application/json"      })    })  }POSTMAN 查詢的結果
查看完整描述

3 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

使用以下代碼啟用 CORS。.NET Core 版本:2.1 和 Angular 版本:1.6.7


public class Startup

{

    public Startup(IConfiguration configuration)

    {

        Configuration = configuration;

    }


    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.AddMvc()

            .AddJsonOptions(options =>

        {

            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

            options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;                

        });


        services.AddCors();

    }


    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)

    { 

        app.UseCors(options =>

        {

            options.AllowAnyMethod();

            options.AllowAnyOrigin();

            options.AllowAnyHeader();

        });


        app.UseMvc();

    }   

}

角代碼:


$http.post("http://localhost:52008/Account/GetJWT", credentials,{

    headers: new HttpHeaders({

        "Content-Type": "application/json"

})).then(

function(response) {

    console.log(response);

},

function() {

    console.log("unable to login");

});

如果問題仍然存在,請告訴我。


查看完整回答
反對 回復 2022-01-16
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

您可以在 startup.cs 中配置核心,如下所示,并將源添加到 ConfigurationServices,如下所示。


public void ConfigureServices(IServiceCollection services)

{

    services.AddCors(options =>

    {

        options.AddPolicy("AllowSpecificOrigin",

            builder => builder.WithOrigins("http://example.com"));

    });

}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, 

    ILoggerFactory loggerFactory)

{

    loggerFactory.AddConsole();


    if (env.IsDevelopment())

    {

        app.UseDeveloperExceptionPage();

    }


    // Shows UseCors with named policy.

    app.UseCors("AllowSpecificOrigin");


    app.Run(async (context) =>

    {

        await context.Response.WriteAsync("Hello World!");

    });

}

將 CORS 策略添加到特定操作。


[HttpGet]

[EnableCors("AllowSpecificOrigin")]

public IEnumerable<string> Get()

{

    return new string[] { "value1", "value2" };

}


查看完整回答
反對 回復 2022-01-16
?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

    public void ConfigureServices(IServiceCollection services)

        {

           //add cors service

            services.AddCors(options => options.AddPolicy("Cors",

                builder => {

                    builder.AllowAnyOrigin()

                    .AllowAnyMethod()

                    .AllowAnyHeader();

                } ));


            services.AddMvc(); // 

//--------------------------------------


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.





 public void Configure(IApplicationBuilder app, IHostingEnvironment env)

    {

        if (env.IsDevelopment())

        {

            app.UseDeveloperExceptionPage();

        }

        app.UseCors("Cors");//------>let app use new service

        app.UseMvc();


    }

在您的控制器內部,確保您從身體上抓取物體


//發(fā)布請求


 [HttpPost]

    public Message Post([FromBody] Message message)

    {


    var msg = new Message { Owner = message.Owner, Text = message.Text };

    db.Messages.AddAsync(msg);

    db.SaveChangesAsync();


    return message;


}


查看完整回答
反對 回復 2022-01-16
  • 3 回答
  • 0 關注
  • 419 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號