How to redirect with Flask and jQuery How to redirect with Flask and jQuery flask flask

How to redirect with Flask and jQuery


Redirects on ajax calls don't work, browsers do not honor them. Most people implement their own custom redirect solution, by returning code 200 and a URL to redirect to in the JSON response. See this question for some good examples.

As a side note, POSTing the login form via ajax does not really give you much of an advantage, I would let the browser POST the form normally and then your Flask view function redirects to a new page on success or redirects back to the login page on failure, possibly with a flash message to inform the user of the error.


No reason use redirect with ajax because it will return redirected content (in your case just content with index endpoint). So you can return link to redirect if all ok (status 200) instead real redirect:

return url_for('index')

and process it with your js:

var successFunction = function (data) {    // do something    window.location = data;};