Skip to content

Instantly share code, notes, and snippets.

@savicas
Created October 29, 2019 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savicas/3d65132cb181f2b4242de491373c927c to your computer and use it in GitHub Desktop.
Save savicas/3d65132cb181f2b4242de491373c927c to your computer and use it in GitHub Desktop.
<!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