From fe49494a1a712507f247391fb04b50ca4d4a5e4d Mon Sep 17 00:00:00 2001 From: BHONESH CHAWLA Date: Mon, 15 Oct 2018 14:01:04 +0530 Subject: [PATCH] Update background.js added provideJoke() --- background.js | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/background.js b/background.js index f2fc661..d671e16 100644 --- a/background.js +++ b/background.js @@ -281,7 +281,45 @@ $(document).ready(function(){ function updateRec() { } - function provideJoke(){ + function provideJoke() + { + + function loadJSON(path, success, error) + { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() + { + if (xhr.readyState === XMLHttpRequest.DONE) + { + if (xhr.status === 200) { + if (success) + success(JSON.parse(xhr.responseText)); + } + else + { + if (error) + error(xhr); + } + } + }; + xhr.open("GET", path, true); + xhr.send(); + } + + loadJSON('https://08ad1pao69.execute-api.us-east-1.amazonaws.com/dev/random_joke', + function(data) { + console.log(data.setup) + console.log(data.punchline) + document.getElementById("joke").innerHTML = data.setup; + document.getElementById("joke-answer").innerHTML = data.punchline; + + }, + function(xhr) { + console.error(xhr); + } + ); + + // Complete the execution of this function to return a joke // You may use APIs like http://api.icndb.com/jokes/random }; @@ -723,4 +761,4 @@ $(document).ready(function(){ buttons[i].addEventListener('click', remove); }; } -}); \ No newline at end of file +});