Skip to content

Instantly share code, notes, and snippets.

@deadkff01
Last active August 15, 2018 14:38
Show Gist options
  • Save deadkff01/f17f3ae17f7d03e17032030d7522a3a3 to your computer and use it in GitHub Desktop.
Save deadkff01/f17f3ae17f7d03e17032030d7522a3a3 to your computer and use it in GitHub Desktop.
Simple get with fetch API
<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