-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c64b9e
commit 5fa645e
Showing
1 changed file
with
46 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
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); | ||
}) | ||
}) | ||
|
||
} |