我在使本机应用程序与宁静的api后端通信时遇到问题。
这是后端控制器
[ApiController]
[Route("[controller]")]
public class AuditController : ControllerBase
{
[HttpGet]
public IActionResult Audits()
{
var audit = new List<AuditRequest>
{
new AuditRequest
{
Date = "23/04/2019 16:49:37",
User ="Fiona",
Message="Logon"
},
new AuditRequest
{
Date = "23/04/2019 16:49:37",
User ="Fiona",
Message="Logon"
},
new AuditRequest
{
Date = "23/04/2019 16:49:37",
User ="Fiona",
Message="Logon"
}
};
return Ok(audit);
}
}
启动文件
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(opt =>
{
opt.AddPolicy("CorsPolicy", policy =>
{
policy.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
//.WithOrigins("http://localhost:3000");
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("CorsPolicy");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
和我的反应文件。
useEffect(() => {
axios
.get("https://localhost:44354/audit")
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
console.log("error");
});
//auditsActions.fetchAudits();
}, []);
我可以使用Postman测试终点。
我得到的错误是网络错误
有人可以告诉我如何解决此问题吗?
答案 0 :(得分:0)
最后,我使用了此应用程序-keyoti的传送带。