I am new in Javascript and I get the following problem. So I make a simple Flask API return a simple json
Flask code:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/test')
def test():
return jsonify({"result":"wt is works!"})
if __name__=="__main__":
app.run()
I run it using gunicorn -k gevent -w 5 main:app
and when I check localhost:8000/test
in browser it returns the json normally
But when I try to get it using Jquery it return nothing. My HTML didn't get any value from localhost:8000/test
My current html code:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type=text/javascript>
$(document).ready(function(){
$.getJSON(
"http://localhost:8000/test",
{format: "json"})
.done(
function(data) {
var plot_id = data.result;
$("#retval").html( "<strong>" + plot_id + "</strong>" );
}
);
});
</script>
</head>
<body>
<div id="retval"></div>
</body>
</html>
But when I change the url and try to get data from another API, let say from https://jsonplaceholder.typicode.com/posts/1 it works.
I've tried solution from:
- Python Flask get json data to display (without
.done()
and using.ajax
) - External API GET() request using jQuery (using
callback=?
) - And also tried to run using
python
command instead ofgunicorn
but it still doesn't work, can somebody help me please to find out what is the problem?