I have built a platform hosted on Pythonanywhere, written with the Flask web framework. In a Javascript file for a specific page, I have written an $.ajax
request to a specific route in my app.py
. This request works fine when I run my app on my personal machine, however, when I moved the app to Pythonanywhere, everytime the $.ajax
request is fired, the request is duplicated, as evidenced by a duplicate database insertion and duplicate response in the javascript. I have tried using event.stopPropagation
, checking that the div
class
is not bound to multiple handlers, etc, yet nothing works. I also posted about this issue over on Stackoverflow (post here), however, I think the problem may lie with the specific app on Pythonanywhere, since the request is only sent once on my machine. Does anyone know how this error might be resolved? Thank you very much!
A simplified example of my code:
In frontend.html
:
<div class='wrapper'>
<div class='submit_stamp' data-timestamp='2019-8-2'>Submit</div>
</div>
In frontend.js
:
$('.wrapper').on('click', '.submit_stamp', function(){
$.ajax({
url: "/submit_time",
type: "get",
data: {time: $(this).data('timestamp')},
success: function(response) {
$('.wrapper').append(response.html);
},
});
});
In app.py
:
@app.route('/submit_time')
def submit_time():
db_manager.submit_stamp(flask.request.args.get('time'))
return flask.jsonify({'html':'<p>Added timestamp</p>'})