0
Hello, I am developing an application using as Backend ASP.NET C#, and with front Angular5! I’m doing a post method for the first time and it’s not flowing well!
In Backend I have a method as follows:
[HttpPost, Route("api/usuario/novo")]
public void Post([FromBody]Usuario usuario)
{
_usuarioService.Adicionar(usuario);
}
That is, it is a Post method, which will receive a user and will add the same...
At Angular I have the following request
inserindoUsuario(usuarios: any){
this.usuariosModel = {
UsuarioId: null,
Nome: usuarios.nome,
Sobrenome: usuarios.sobrenome,
Email: usuarios.email,
Senha: usuarios.senha,
Corretor: usuarios.corretor,
Contrato: usuarios.contrato
}
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.Http.post('http://localhost:57301/api/usuario/novo', JSON.stringify(this.usuariosModel), { headers: headers })
.subscribe(() => {
console.log(this.usuariosModel);
});
}
That is, I create a user, mount the request header and call the Subscribe... However in the browser console shows the following error:
Failed to load http://localhost:57301/api/usuario/novo: Response to
preflight request doesn't pass access control check: No 'Access-
Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4200' is therefore not allowed access. The
response had HTTP status code 405.
But my Header is already configured..
Give a search on how to leave Cors in your backend
– Eduardo Vargas