Last active
August 15, 2018 14:38
-
-
Save deadkff01/f17f3ae17f7d03e17032030d7522a3a3 to your computer and use it in GitHub Desktop.
Simple get with fetch API
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
<button type="button" id="btn">Show my IP</button> | |
<div id="ipContainer"></div> | |
<script> | |
const btn = document.getElementById('btn') | |
const ipContainer = document.getElementById('ipContainer') | |
const fetchJSON = async (req) => { | |
const request = await fetch(req) | |
return request.json(); | |
} | |
btn.addEventListener('click', async () => { | |
ipContainer.innerHTML = "searching..." | |
const data = await fetchJSON('http://httpbin.org/ip') | |
ipContainer.innerHTML = `<h1>${data.origin}</h1>` | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment