Skip to content

Instantly share code, notes, and snippets.

@alexishida
Created September 22, 2016 03:46
Show Gist options
  • Select an option

  • Save alexishida/bd45ff82312eb2ad8a7187e542b9036a to your computer and use it in GitHub Desktop.

Select an option

Save alexishida/bd45ff82312eb2ad8a7187e542b9036a to your computer and use it in GitHub Desktop.
jQuery’s JSONP API GitHub example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Teste JSONP - API do GITHUB</title>
<link rel="shortcut icon" href="/favicon.ico" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<script src='https://code.jquery.com/jquery-3.1.0.min.js'></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
consutaDados();
});
function consutaDados(){
$("#exibicao").html('');
$("#exibicao").append('<p id="carregando"> Carregando... </p>');
$.ajax({
url: 'https://api.github.com/users/alexishida',
type: 'GET',
async: false,
dataType: 'jsonp',
success: function(retorno) {
$("#carregando").hide();
mostrarAvatar(retorno);
mostrarDados(retorno)
console.log(retorno); // serve para debugar
},
error: function(dados) {
$("#carregando").hide();
alert("Ocorreu um erro na busca dos dados, favor verifique sua conexão!");
}
});
}
function mostrarAvatar(dados){
$("#exibicao").append('<img src="'+ dados.data.avatar_url +'" width="233" height="233" />');
}
function mostrarDados(dados){
$("#exibicao").append('<p>Nome Completo:<b> '+ dados.data.name +'<b></p>');
$("#exibicao").append('<p>Cidade:<b> '+ dados.data.location +'<b></p>');
$("#exibicao").append('<p>Bio:<b> '+ dados.data.bio +'<b></p>');
$("#exibicao").append('<a href="'+ dados.data.html_url +'" target="_blank">Ir para o Github</a>');
}
</script>
<div id="exibicao">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment