Saturday, 24 September 2016

jquery - Flask how to redirect to new page after ajax call





I try to redirect to a new page after ajax post but I have not been successfull yet.



My template looks like this:







Html part:




response1




response2








And on the server side flask application looks like this:




#!flask/bin/python

import sys

from flask import Flask, render_template, request, redirect, Response, url_for
import random, json

app = Flask(__name__)


@app.route('/')
def index():
return render_template('index.html')

@app.route('/somepage')
def somepage():
return 'Success'


@app.route('/receiver', methods = ['POST'])

def worker():

data = request.get_json()
result = ''
result = str(data)

print(result)

return (redirect(url_for('somepage')))



if __name__ == '__main__':

app.run(debug=True)


When I open Chrome developer tools and click network it looks fine and I may see success message but stil browser doesn't redirects to /somepage


Answer



As I see in your flask application you try to output the data and redirect to home page, but the application will execute the output part which is the message and will not continue to the redirect,
if you need to make a redirect, you should do it in ajax call.




success: function(){
window.location.href = "somepage";
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...