Logout request from Javascript to Flask

I’m trying to logout user from my web application using Flask-Login.

Here’s my Javascript function:

function logout() {
  var req = new XMLHttpRequest();
  req.open("POST", "http://myaddress/logout", true);
  req.withCredentials = true;
  req.send();

Here’s my Python function:

@app.route('/logout')
@login_required
def logout_function():
    logout_user() # Built-in Flask-Login function
    return app.send_static_file('login.html')

When I send my request from Javascript I receive 200, but the page remains still, because redirect doesn’t happen. I tried changing the last line of Python function to return redirect(url_for('login_function')), but it didn’t help.

Could it be my Javascript function that causes the problem?

13 thoughts on “Logout request from Javascript to Flask”

Leave a Comment