简体   繁体   中英

Read status code on response from asp.net core in angular 6

I have an API in Asp.net core 2 and I need catch the status code from the response, but on my response in angular only have my API result.

My method on API

[HttpPost]
    [Route("cadastrar-categoria")]
    public async Task<IActionResult> CadastrarCategoriaAsync([FromBody]Categoria item)
    {
        try
        {
            var sucesso = await _service.CadastroCategoria(item);
            if (sucesso == 1)
            {
                return Ok("Sucesso");
            }
            else
            {
                return BadRequest("Erro");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

And my Service in angular

CadastrarCategoria(nome: string) {
 let data = new CadastroCategoriaDto();
 data.Nome = nome;

 return this.req
   .post(environment.urls.cadastrarProduto, data)
   .subscribe((res: Response) => {
     console.log(res);
   });

}

My response in the browser is only this. enter image description here

How I catch the status code from this response? example in return Ok() I need receive status code 200.

If you want full response in subscribe method then use {observe: 'response'} .

Example:

return this.req
   .post(environment.urls.cadastrarProduto, data,{observe: 'response'})
   .subscribe((res: HttpResponse) => {
     console.log(res.status);
   });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM