Get Header information, javascript

Asked

Viewed 565 times

0

Good morning, I need to get the statuscode that returns in the header, after I run a POST , I would like to know a way to achieve this, using java script if possible what is the best way to get the status that the api returns to me directly from the header?

inserir a descrição da imagem aqui

2 answers

1


Using the object XMLHttpRequest javascript itself, would be as follows:

    const xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {    
        console.log(this.status); //Status Code

        //Successfully
        if (this.readyState == 4 && this.status == 200) {
            //TODO
        }
    };

    xhttp.open("POST", "ENDPOINT_API", true);
    xhttp.send();

0

By making a post using Jquery, you can get the status in case of error. From Jquery 3.0, you would do so:

$.ajax("minha_pagina.php")
  .done(function(data) {
      //Sucesso
      console..log(data);
  })
  .fail(function(xhr, status, error) {
      //Erro
      var errorMessage = xhr.status + ': ' + xhr.statusText
      alert('Error - ' + errorMessage);
})

Here is more information:

http://api.jquery.com/jquery.ajax/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.