
[英]ajax post request - cross-Origin Read Blocking (CORB) blocked cross-origin response CORS
[英]Cross Origin Request Blocked on HTTP POST request
我正在将 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);
}
}
不工作的 POST 请求
public class Form
{
string title;
string summary;
string body;
string[] tags;
}
[Route("api/submitForm")]
[ApiController]
public class FormController : ControllerBase
{
[HttpPost]
public void Post([FromBody] Form formData)
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(formData);
}
}
FormController 类
就像我之前说的,它适用于对我的 SearchController 的 GET 请求。 但是由于某种原因,我在对 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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.