diff --git a/src/index.js b/src/index.js index d6fa599..c4cf2ff 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,51 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const url='https://rickandmortyapi.com/api/character/'; -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); +const fetchData=()=>{ + fetch(url) -function fetchData(url_api, callback) { - xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === '4') { - if (xhttp.status == 200) - callback(null, xhttp.responseText); - else return callback(url_api); + .then(data=>data.json()) + .then(data=>{ + + console.log(data); + + for(let i in data.results){ + document.write("id : " +data.results[i].id+ + " "+"Name: "+data.results[i].name+ + "Type: "+data.results[i].type+"

"); + + } + + }) + .catch(()=>{document.write("Personaje no encontrado!")}) + +} + + + +async function getData(){ + + + const data=await fetch('https://rickandmortyapi.com/api/character/') + + try { + if(data.ok){ + const personajes=await data.json(); + console.log(personajes); + + + for(let i in personajes.results){ + document.write("Nombre: " +personajes.results[i].name+ + " "+"Status: "+personajes.results[i].status+ + "Type: "+personajes.results[i].type+"

"); + } + + } + } catch (error) { + console.log(error) } - }; - xhttp.open('GET', url_api, false); - xhttp.send(); -}; - -fetchData(API, function (error1, data1) { - if (error1) return console.error('Error' + ' ' + error1); - console.log('Primer Llamado...') - fetchData(API + data1.results[0].id, function (error2, data2) { - if (error2) return console.error(error1); - console.log('Segundo Llamado...') - fetchData(data2.origin.url, function (error3, data3) { - if (error3) return console.error(error3); - console.log('Tercero Llamado...') - console.log('Personajes:' + ' ' + data1.info.count); - console.log('Primer Personaje:' + ' ' + data2.name); - console.log('Dimensión:' + ' ' + data3.dimension); - }); - }); -}); \ No newline at end of file + + + +} + +getData() \ No newline at end of file