后端在startup里边加跨域配置如下:
public void ConfigureServices(IServiceCollection services)
{
//配置跨域处理,允许所有来源
services.AddCors(options =>
{
options.AddPolicy("any", builder =>
{
builder.WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS") //允许任何来源的主机访问
.AllowAnyOrigin()
.AllowAnyHeader();
//.AllowCredentials();//指定处理cookie
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
//跨域配置
app.UseCors("any");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers().RequireCors("any");
});
}