Muc*_*sia 1 asp.net cors axios
我正在 ASP .NET Core 2.1 Web API 和 React 中编写应用程序。我的服务器在 localhost:5000 上,我的客户端在 localhost:3000 上。我想用 axios 发送 post 请求,但在浏览器控制台中我看到错误:
OPTIONS https://localhost:5001/api/auth/login 0 ()
Uncaught (in promise) Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:87)
Run Code Online (Sandbox Code Playgroud)
在我的 Web API 中,我尝试添加 CORS,但它似乎不起作用。我加:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
...
}
Run Code Online (Sandbox Code Playgroud)
和:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(options =>
options
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
...
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
这是我的帖子请求:
const response = await axios({
method: 'post',
url: 'http://localhost:5000/api/auth/login',
data: {
userName: this.state.controls.login.value,
password: this.state.controls.password.value
}
});
Run Code Online (Sandbox Code Playgroud)
请求后的服务器控制台:
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 OPTIONS http://localhost:5000/api/auth/login
info: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
Policy execution successful.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 4.253ms 204
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 POST http://localhost:5000/api/auth/login application/json;charset=UTF-8 52
info: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
Policy execution successful.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 2.1805ms 307
info: Microsoft.AspNetCore.Server.Kestrel[32]
Connection id "0HLG4CH3JIV16", Request id "0HLG4CH3JIV16:00000002": the application completed without reading the entire request body.
Run Code Online (Sandbox Code Playgroud)
当我使用 Postman 时,一切都很好。我从服务器收到带有令牌的 json。
| 归档时间: |
|
| 查看次数: |
4548 次 |
| 最近记录: |