Created
October 29, 2019 15:37
-
-
Save savicas/3d65132cb181f2b4242de491373c927c to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Random Ron</title> | |
<style type="text/css"> | |
body { | |
margin: 1em auto; | |
max-width: 40em; | |
width: 88%; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Random Ron</h1> | |
<blockquote id="saying"></blockquote> | |
<p> | |
<button>Another quote from Ron</button> | |
</p> | |
<script crossorigin="anonymous" src="https://polyfill.io/v3/polyfill.min.js"></script> | |
<script> | |
document.querySelector('button').addEventListener('click', function(event){ | |
// Fetch | |
fetch('http://ron-swanson-quotes.herokuapp.com/v2/quotes', { | |
// method: 'POST', | |
// body: 'title=' + encodeURIComponent('My awesome new article') + '&body=' + encodeURIComponent('This is the text of my article') | |
}).then(function (response) { | |
// The API call was successful! | |
if (response.ok) { | |
return response.json(); | |
} else { | |
return Promise.reject(response); | |
} | |
}).then(function (data) { | |
// This is the JSON from our response | |
console.log(data) | |
document.querySelector('#saying').innerHTML = data; | |
}).catch(function (err) { | |
// There was an error | |
console.warn('Something went wrong.', err); | |
}); | |
}, false); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment