【发布时间】:2021-03-28 14:33:02
【问题描述】:
这是我在 asp.net core web API 中的配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
和
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors("CorsPolicy");
最后是我的 javascript(打字稿反应):
const requestOptions = {
method: 'POST',
mode: 'cors' as RequestMode,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ Username: username, Password: password })
};
return fetch(`${this.baseUrl}/auth/register`, requestOptions).then(res => {
debugger;
if (res.ok === false) {
return Promise.reject("Invalid request");
}
return res.json();
});
我想不通。我试过改变客户端的模式。我已经在 API 中尝试过.WithOrigins("http://localhost:3000") ,但我现在不走运了。我在这里想念什么?我用 npm start 运行了我的 react 应用程序,是这样吗?不应该吗?
回复:
Request URL: https://localhost:44346/auth/register
Request Method: POST
Status Code: 404
Remote Address: [::1]:44346
Referrer Policy: strict-origin-when-cross-origin
响应头:
access-control-allow-origin: *
date: Thu, 17 Dec 2020 22:23:37 GMT
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET
【问题讨论】:
-
当您请求 不是 404 的内容时,您是否也会收到此错误?
-
什么确切是cors错误信息?
-
你好@KevinB,这很奇怪。我在 OPTION 调用(预检)中得到 204,但在 POST 调用中得到 404。响应和响应标头发布在帖子中
-
是的,但是,实际的错误是什么?如果没有实际的 cors 错误,我们只能推测浏览器拒绝它的原因。
-
您的标题似乎是有序的,您的最后一条评论似乎证实预检没有失败,所以...我怀疑这可能根本不是与 cors 相关的问题。跨度>
标签: reactjs typescript asp.net-core asp.net-web-api react-typescript