Skip to content

Instantly share code, notes, and snippets.

@willianantunes
Created August 11, 2018 13:44
Show Gist options
  • Save willianantunes/7025975e21de72713df136bf25d4e0d9 to your computer and use it in GitHub Desktop.
Save willianantunes/7025975e21de72713df136bf25d4e0d9 to your computer and use it in GitHub Desktop.
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