Created
November 15, 2020 12:25
-
-
Save kaustubhgupta/ee36b0fb856b1536ded893ca9a10e539 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<body onload="brython()"> | |
<h1>Get a joke</h1> | |
<button id="joke-btn">Get Joke</button> | |
<div id="joke" class="card">Click the "get joke" button</div> | |
<!-- Ajax call --> | |
<script type="text/python" id="script3"> | |
from browser import document, ajax | |
url = 'https://api.chucknorris.io/jokes/random' | |
def on_complete(req): | |
import json | |
data = json.loads(req.responseText) | |
joke = data['value'] | |
document['joke'].text = joke | |
def get_joke(e): | |
req = ajax.ajax() | |
req.open('GET', url, True) | |
req.bind('complete', on_complete) | |
document['joke'].text = 'Loading...' | |
req.send() | |
document['joke-btn'].bind('click', get_joke) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment