I'm getting the values of this json link but, when I try to get "films" it shows me the value as undefined . I refer to this exact value but I think it is not recognizing that it is a json and that is why it shows the undefined.
The reference is to this value
"films": [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
Annex the code
function get (URL, callback) {
"use strict";
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
//Todo ok
callback(null, JSON.parse(this.responseText));
} else {
//Hubo un error
callback(new Error('Se produjo un error al realizar el request: ${this.status}')); //this apunta al request xhttp
}
}
xhttp.open('GET', URL); //Type of Request
xhttp.send(null); // Send request to server
}
function handleError(err) {
console.log('Request failed: ${err}');
}
get('https://swapi.co/api/people/1/', function onResponde(err, luke) {
if (err) return handleError;
/*Trae el mundo*/
get(luke.homeworld, function onHomeWorldResponse (err, homeworld) {
if (err) return handleError;
/*Traer la especie*/
get(luke.species, function onSpeciesResponse (err, species) {
if (err) return handleError;
get(luke.films[0], function onFilmsResponse(err, films) {
if (err) return handleError;
luke.films[0] = films;
console.log('${luke.name} estuvo en las peliculas ${luke.films.title}');
})
luke.species = species;
console.log('${luke.name} es un ${luke.species.name}');
})
luke.homeworld = homeworld;
console.log('${luke.name} nació en ${luke.homeworld.name}');
})
console.log('Request succeded');
console.log('luke', luke);
console.log("Datos de Sr." + luke.name.substr(0,4) + ":");
console.log('Nombre Completo: ${luke.name}
Género: ${luke.gender}
Estatura: ${luke.height}
Color de Cabello: ${luke.hair_color}
Color de ojos: ${luke.eye_color}');
});