Skip to content

Instantly share code, notes, and snippets.

@peterbsmyth
Created October 29, 2019 22:14
Show Gist options
  • Save peterbsmyth/81bb33c85cf895ae4cd526b1bb67b556 to your computer and use it in GitHub Desktop.
Save peterbsmyth/81bb33c85cf895ae4cd526b1bb67b556 to your computer and use it in GitHub Desktop.
function reqListener() {
var data = JSON.parse(this.responseText);
console.log(data);
}
function reqError(err) {
console.log('Fetch Error :-S', err);
}
var request = new XMLHttpRequest();
request.onload = function() {
var data = JSON.parse(this.responseText);
//do stuff with data
};
request.onerror = function() {
alert('There was a problem with the request');
}
request.open('get', '/api/foo/bar', true);
request.send();
fetch('/api/foo/bar').then(function(data) {
return data.json();
}).then(function(jsonData) {
//do stuff with the data
}).catch(function(e) {
alert('There was a problem with the request');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment