Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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
C# HTTP POST请求阻止跨源请求_C#_Angular_Asp.net Core_.net Core_Asp.net Core Mvc - Fatal编程技术网

C# HTTP POST请求阻止跨源请求

C# HTTP POST请求阻止跨源请求,c#,angular,asp.net-core,.net-core,asp.net-core-mvc,C#,Angular,Asp.net Core,.net Core,Asp.net Core Mvc,我正在将http请求从我的angular客户端应用程序发送到.NET核心web API。虽然启用了CORS,但我收到了CORS错误。当我向我的SearchController发送一个GET请求时,结果很好,但是当我向我的FormController发送一个POST请求时,我得到了CORS错误 我试过在chrome上运行它,同样的结果 public void Configure(IApplicationBuilder app, IHostingEnvironment env) {

我正在将http请求从我的angular客户端应用程序发送到.NET核心web API。虽然启用了CORS,但我收到了CORS错误。当我向我的SearchController发送一个GET请求时,结果很好,但是当我向我的FormController发送一个POST请求时,我得到了CORS错误

我试过在chrome上运行它,同样的结果

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }


            app.UseCors(builder =>
                builder.AllowAnyOrigin()
                );

            app.UseHttpsRedirection();
            app.UseMvc();
 }
如您所见,我已将其配置为允许任何原点

const httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
      'Authorization': 'my-auth-token',

    })
};



@Injectable()

export class SendFormService
{
    submitUrl : string = "https://localhost:5001/api/submitForm";

    constructor(private http : HttpClient){ }

    public SendFormData(formData : RoadmapForm) : Observable<RoadmapForm>
    {
        //remember to error handle
        return this.http.post<RoadmapForm>(this.submitUrl, formData, httpOptions);

    }
}
FormController类

正如我前面所说,它适用于向我的SearchController获取请求。但由于某些原因,我在向FormController发送POST请求时收到了CORS错误。

启用CORS

public void ConfigureServices(IServiceCollection services)
{
      services.AddCors();
      services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseCors(
        options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()
    );

    app.UseMvc();
}

POST请求响应的HTTP状态代码是什么?使用浏览器devtools中的网络窗格进行检查。这是4xx还是5xx错误,而不是200 OK成功响应?非常感谢。事实证明,在AddMvc()不知道顺序的重要性之后,我有了AddCors()。
public void ConfigureServices(IServiceCollection services)
{
      services.AddCors();
      services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseCors(
        options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()
    );

    app.UseMvc();
}