Good afternoon, I am making use of the Javascript AXIOS library for calls to the server and consumption of an API. Everything works fine, but I need to know how the elements of the JSON result can be put into variables to access them.
For example, with HTTPRequest this code was used:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", $urlEnvio, false);
xmlhttp.setRequestHeader("accept" ,"application/json");
xmlhttp.setRequestHeader("content-type" ,"application/json");
xmlhttp.send($urlEnvio);
var resultado = xmlhttp.response;
var elementos =JSON.parse(resultado);
This made me request the API and then I inserted the results in the variable elements, being able to access one of them by entering elements [0] or the position that was.
On the other hand, with AXIOS the fragment works correctly, but the part of JSON.parse used with HTTPRequest I can not apply it. Here is the fragment of my application with AXIOS:
axios.get(urlEnvio, usuario, clave)
.then(response => {
// JSON responses are automatically parsed.
this.posts = response.data
//var elementos = JSON.stringify(posts);
alert(posts[0]);
})
In the alert I get the UNDEFINED message. I appreciate any help, thank you very much.