I am trying to get the logic of using jQuery AJAX and Flask, however I am not coming right. I have been following tutorials here and here, but no resolution. To get the logic - I simply want to input dates in two separate fields, and return those dates to the same page in a normal <p>
tag. My suspicion is a problem in the server receiving the dates. Submitting the relative form does not return anything to #result
HTML FORM
<form>
<input name="StartDate" id="StartDate" type="date" class="form-control"></div>
<input name="EndDate" id="EndDate" type="date" class="form-control"></div>
<a href="#" id="generate"><button class='btn btn-info'>Generate</button></a>
</form>
<p id=result></p>
jQuery - located at the bottom of the <body>
tag, after importing jQuery itself.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('a#generate').bind('click', function () {
$.getJSON($SCRIPT_ROOT + '/generate_report', {
StartDate: $('input[name="StartDate"]').val(),
EndDate: $('input[name="EndDate"]').val()
}, function (data) {
$("#result").text(data.result);
});
return false;
});
});
</script>
Flask Server - I have imported relevant dependencies
@app.route('/generate_report', methods=['GET', 'POST'])
def generate_report():
start_date = request.args.get('StartDate', 0, type=str)
end_date = request.args.get('EndDate', 0, type=str)
return jsonify(result=start_date + "-" + end_date)
question from:
https://stackoverflow.com/questions/66060275/simple-flask-jquery-ajax 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…