Skip to content

Commit

Permalink
Feito resultado do exercício
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasdaiki committed Jun 20, 2017
1 parent 1c64b9e commit 5fa645e
Showing 1 changed file with 46 additions and 15 deletions.
61 changes: 46 additions & 15 deletions aulas/aula2/client/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,61 @@
const result = document.querySelector('.result');

function openXMLHttpRequest() {
// function openXMLHttpRequest() {

const xhr = new XMLHttpRequest(),
method = 'GET',
url = 'http://localhost:3000';
// const xhr = new XMLHttpRequest(),
// method = 'GET',
// url = 'http://localhost:3000';

xhr.open(method, url, true);
// xhr.open(method, url, true);

// xhr.onreadystatechange = function () {

// /*

// 0 UNSENT Um cliente foi criado. Mas o método open() não foi chamado ainda.
// 1 OPENED O método open() foi chamado.
// 2 HEADERS_RECEIVED o método send() foi chamado e os cabeçalhos e status estão disponíveis .
// 3 LOADING Baixando e responseText contem os dados parciais.
// 4 DONE Operação concluída.

xhr.onreadystatechange = function () {
// */

/*
// if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// result.innerHTML = JSON.stringify(JSON.parse(xhr.responseText), null, 2);
// }
// }

0 UNSENT Um cliente foi criado. Mas o método open() não foi chamado ainda.
1 OPENED O método open() foi chamado.
2 HEADERS_RECEIVED o método send() foi chamado e os cabeçalhos e status estão disponíveis .
3 LOADING Baixando e responseText contem os dados parciais.
4 DONE Operação concluída.
// xhr.send();
// }

*/
let HttpService = (method, url, callback) => {
callback = callback || function(){};
const xhr = new XMLHttpRequest();

xhr.open(method, url, true);

xhr.onreadystatechange = function() {

if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
result.innerHTML = JSON.stringify(JSON.parse(xhr.responseText), null, 2);
callback(JSON.parse(xhr.responseText));
}
}
};

xhr.send();
}

function openXMLHttpRequest() {

HttpService('GET', 'https://jsonplaceholder.typicode.com/albums', albums => {
HttpService('GET', 'https://jsonplaceholder.typicode.com/photos', photos => {
const relationship = albums.map(album => {
album.photos = photos.filter(photo => photo.albumId === album.id)

return album;
})

result.innerHTML = JSON.stringify(relationship, null, 2);
})
})

}

0 comments on commit 5fa645e

Please sign in to comment.