Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<!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
You can’t perform that action at this time.