Created
August 11, 2018 13:44
-
-
Save willianantunes/7025975e21de72713df136bf25d4e0d9 to your computer and use it in GitHub Desktop.
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
class HttpService { | |
get(url) { | |
return new Promise((resolve, reject) => { | |
let xhr = new XMLHttpRequest(); | |
xhr.open('GET', url); | |
xhr.onreadystatechange = () => { | |
if (xhr.readyState == 4) { | |
if (xhr.status == 200) { | |
resolve(JSON.parse(xhr.responseText)); | |
} else { | |
reject(xhr.responseText); | |
} | |
} | |
}; | |
xhr.send(); | |
}); | |
} | |
post(url, dado) { | |
return new Promise((resolve, reject) => { | |
let xhr = new XMLHttpRequest(); | |
xhr.open("POST", url, true); | |
xhr.setRequestHeader("Content-type", "application/json"); | |
xhr.setRequestHeader("Accept", "application/json") | |
xhr.onreadystatechange = () => { | |
if (xhr.readyState == 4) { | |
if (xhr.status == 200) { | |
resolve(JSON.parse(xhr.responseText)); | |
} else { | |
reject(xhr.responseText); | |
} | |
} | |
}; | |
xhr.send(dado === undefined ? null : JSON.stringify(dado)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment