-
-
Save aliams/2b51762b48a8673599823135cc604f74 to your computer and use it in GitHub Desktop.
Simple fetch vs. XHR example
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
fetch('weather.json') | |
.then(function(response) { | |
if (response.headers.get('content-type').includes('json')) { | |
return response.json(); | |
} else { | |
throw new TypeError(); | |
} | |
}) | |
.then(processJSON); |
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
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', 'weather.json'); | |
xhr.responseType = 'json'; | |
xhr.onload = function() { | |
if (xhr.status == 200 | |
&& xhr.getResponseHeader('Content-Type').indexOf('json') !== -1) { | |
processJSON(xhr.response); | |
} else { | |
throw new TypeError(); | |
} | |
} | |
xhr.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment