From 0f0a89b83cff7b6972e3f6ed156c7161566c0f52 Mon Sep 17 00:00:00 2001 From: subutai1175 Date: Tue, 10 Oct 2017 13:25:26 +0800 Subject: [PATCH] fetch random quote --- index.html | 19 +++++++++++++++++++ script.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 index.html create mode 100644 script.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..0e99b8a --- /dev/null +++ b/index.html @@ -0,0 +1,19 @@ + + + + + Random Quotes + + +

Random Quotes

+ + + + + + diff --git a/script.js b/script.js new file mode 100644 index 0000000..2d48d69 --- /dev/null +++ b/script.js @@ -0,0 +1,53 @@ +$(function(){ + + var myHeader = new Headers({ + "X-Mashape-Key": "c4vYgfOINAmshnnJighZi26DLxMcp1NUYY7jsnRkY6RVXbKLHY", + }); +// console.log(myHeader); + var myInit = { + method: 'POST', + headers: myHeader + }; + + var url = "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous&count=10" + + var ul = $('ul'); + var $button = $('button') + + $button.on('click', function(){ + fetch(url, myInit) + .then((response) => { //when we get response back + // console.log('response received') + // console.log(response.json()) + return response.json() + }) + .then(data => { + data.forEach(function (element) { + var quote = element.quote; + var author = element.author; + var $p = $('

').text(`Author: ${author}`) + var $li = $('

  • ').text(`Quote: ${quote}`); + ul.append($li, $p) + }) + // console.log('data processed and converted into json'); + // console.log(data); + }) + .catch((err) => { + // console.log('error happened somewhere') + // console.log(err); + }) + }); + + + + + + + + + + + + + +});