Last active
September 23, 2015 13:57
-
-
Save Rich-Harris/0af262f1bdebaeebfe2c to your computer and use it in GitHub Desktop.
XHR demo
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
function get ( url ) { | |
return new Promise( function ( fulfil, reject ) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open( 'GET', 'my-data.json' ); | |
xhr.onload = function () { | |
fulfil( xhr.responseText ); | |
}; | |
xhr.onerror = reject; | |
xhr.send(); | |
}); | |
} |
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
<main>hello <span class='target'>[click me to load my-data.json]</span>!</main> | |
<script src='get.js'></script> | |
<script> | |
var target = document.querySelector( '.target' ); | |
target.addEventListener( 'click', function () { | |
get( 'my-data.json' ) | |
.then( JSON.parse ) | |
.then( function ( data ) { | |
target.textContent = data.hello; | |
}); | |
}); | |
</script> |
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
{ | |
"hello": "world" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment