Created
March 7, 2017 15:19
-
-
Save brunogamacatao/88f24423b30e507c018959f9de64b167 to your computer and use it in GitHub Desktop.
Exemplo realizando uma requisição a uma API rest remota
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<!-- área do catálogo de filmes --> | |
<div class="container"> | |
<div class="row"> | |
<div id="id01"></div> | |
</div> | |
</div> | |
<script> | |
function chamarAjax(url, callback) { | |
var request = new XMLHttpRequest(); | |
request.open('GET', url, true); | |
request.onload = function() { | |
if (request.status >= 200 && request.status < 400) { | |
callback(request.responseText); | |
} else { | |
console.log('Houve um erro!'); | |
} | |
}; | |
request.onerror = function() { | |
console.log('Houve um erro!'); | |
}; | |
request.send(); | |
} | |
function montarVideos(response) { | |
var arr = JSON.parse(response); | |
var ancoras = arr.map(function(obj) { | |
return "<a href='#' class='samba-playlist-trigger list-group-item active' data-mediaid='" + obj.id + "'>informações montadas aqui</a>"; | |
}).join(''); | |
document.getElementById("id01").innerHTML = ["<div>", ancoras, "</div>"].join(''); | |
} | |
// MUDEI A URL | |
chamarAjax('https://jsonplaceholder.typicode.com/posts', montarVideos); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment