(This is probably a dumb question, so please wear your stupidity shields!) I've been a PHP programmer and am now learning Python + Flask. I recently had to struggle a lot with posting data through AJAX and returning a response. Finally, the code that worked was:
@app.route('/save', methods=['POST'])
def save_subscriptions():
if request.method == 'POST':
sites = request.form.get('selected')
print(sites)
sites = sites[0:-1]
g.cursor.execute('UPDATE users SET sites = %s WHERE email = %s', [sites, session.get('email')])
g.db.commit()
return json.dumps({'status': 'success'})
If I change return json.dumps({'status': 'success'})
to return 1
I get an Exception that int is not callable
. First of all, I don't understand who is trying to call that int
and why? Secondly, in PHP, it was frequently possible to just echo 1;
and this would become the AJAX response. Why doesn't return 1
work in Flask, then?