0
I’m trying to solve the following exercise:
Create a screen with one that should be named after a user on Github. After typing the
user name and click search button. The application should search for the Github API (as per
URL below) the user repositories data and show them on screen:
Example URL: https://api.github.com/users/diego3g/repos
Just change "diego3g" by user name. After filling in the input and adding, the information must fill in a list.
I can not understand why to run before I click the send and still go straight to the . catch.
I appreciate all your help!
var lista = " ";
var linha = " ";
var perfil = " ";
var textoLinha = " ";
var minhaPromise = function(){
return new Promise(function(resolve, reject){
var x = document.getElementById("user").value;
var xhr = new XMLHttpRequest();
xhr.open("GET", 'https://api.github.com/users/'+x+'/repos');
xhr.send('null');
xhr.onreadystatechange = function(){
if(xhr.readyState === 4) {
if (xhr.status === 200){
resolve();
} else {
reject();
}
}
}
});
}
minhaPromise()
.then(function() {
lista = document.querySelector('#corpo ul');
perfil = JSON.parse(xhr.responseText) || [];for(var dados of perfil){
linha = document.createElement('li');
textoLinha = document.createTextNode(Object.values(dados));
linha.appendChild(textoLinha);
lista.appendChild(linha);
}
})
.catch(function(){
lista = document.querySelector('#corpo ul'); linha = document.createElement('li');
textoLinha = document.createTextNode("Não encontrado.");
linha.appendChild(textoLinha);
lista.appendChild(linha);
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="desafio2.js"></script>
</head>
<body>
<div id="corpo">
<input type="text" name="nome" id="user" />
<input type="submit" value="Enviar" onclick="minhaPromise()" />
<ul></ul>
</div>
</body>
</html>