I have encountered baffling behavior when running an Ajax request as part of my Flask application. I have written a handler receive a div
click and then send an Ajax request with certain data to a specific route specified in my app.py
. The data is then inserted into a database. While this approach worked fine when running my Flask app on my own machine, upon moving my app to another hosting service (Pythonanywhere), every time I click the div
, the request is being sent twice, as evidenced by the data being inserted twice into the database.
Similar variants of this question have been asked before (here and here, for instance), but those questions all deal with POST
requests, while mine is using a GET
. Additionally, those questions generally involved an HTML form
that was being submitted alongside the POST
request, hence the additional request. However, my code does not have any forms.
My code sample (simplified, but the same in essence to my current efforts):
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>'})
As such, whenever I click the submit_stamp
element, the Ajax request fires twice, the timestamp is inserted twice into my database, and "Added timestamp"
is appended twice to .wrapper
. Some things I have done to fix this include:
Adding an
event.stopPropagation()
in the handlerUsing a boolean flag system where a variable is set to
true
just after the click, and reset tofalse
in thesuccess
handler of the.ajax
. I wrapped the$.ajax
with this boolean in a conditional statement.
None of these patches worked. What confuses me, however, is why $.ajax
is called once when running on my machine, but is called twice when running on the hosting service. Does it have to do with the cache? How can I resolve this issue? Thank you very much!
Edit:
Strangely, the duplicate requests occur infrequently. Sometimes, only one request is made, other times, the requests are duplicated. However, I have checked Network XHR output in Chrome and it is only displaying the single request header.
The access log output (with IPs removed):
<IP1> - - [05/Aug/2019:16:35:03 +0000] "GET /submit_time?payload=.... HTTP/1.1" 200 76 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1" "<IP>" response-time=0.217
<IP2> - - [05/Aug/2019:16:35:05 +0000] "GET /submit_time?payload=.... HTTP/1.1" 200 71 "http://www.katchup.work/create" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36" "<IP2>" response-time=0.198