I am a newbie to Jquery and my script below doesn't produce any output on the HTML page.
Problem:
Within my script, I use the requests library to send a GET request to a Flask URL that returns a JSON object. This object then needs to be sent to the JQuery which would then output the JSON object on a webpage. If I send the data using response.text it works fine, but if I send it using response.json() it outputs nothing. However I would need it to be sent as a JSON object so that I could then extract all the data out of it and put it on the HTML elements.
getajaxtest.py:
import requests
import json
def send_log_ajax():
url = 'http://xx.com/returnjsonobj'
response = requests.get(url)
return response.json()
if __name__ == '__main__':
ajax_var = send_log_ajax()
print(ajax_var)
the HTML and JQuery:
<html>
<head><script
src="https://code.jquery.com/jquery-3.2.1.min.js"></script></head>
<body>
<div style="border:1px solid black;background-color:lightgray">
<p id="transactioninfo">Loading Transaction Information...</p>
</div>
<script>
function getlogs() {
var url = 'www.myserver.com/getajaxtest.py';
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
success: function(data) {
$("#transactioninfo").html(data)
},
error: function(e) {
console.log(e.message);
}
});
}
$(document).ready(function(){
setTimeout(function(){getlogs();}, 2000);
setInterval(function(){getlogs();}, 2000);
});
</script>
</body>
</head>
</html>