1

TL;DR for admins: Not a duplicate. I'm trying to force redirect the user from the Flask backend after completing survey using Flask, but it doesn't work (though from my understanding it should - it even behaves on console level as though as it did, but the browser doesn't reload). I do not want yet to do it on the ajax, I try to keep it as minimal as possible for now, the survey makes correctly a vanilla ajax POST to "/survey" and I want to keep it that way for now. Also, I want to understand the Flask problem instead of replacing it with another solution that I don't fully comprehend yet

I'm trying to learn some basics of Web development, wanting to make a somewhat interesting web app service using Flask as my backend.

My survey subsite of the project looks a little like this:

@app.route("/survey", methods=["GET", "POST"])
@login_required
def survey():
    if request.method == "GET":
        return render_template("survey.html")

    if request.method == "POST":
        x = request.get_json(silent=True)
        print(x) 
        //todo: manipulate on x and classify a user into a group   
        return redirect(url_for("index")) // just for now redirect everybody

I'm a complete AJAX/JS newbie, but I know that my SurveyJS survey website ("/survey") after a bit of tinkering is sending the correct JSON to the backend (by printing the vast x variable after completion of the survey). Sadly, the redirects don't work, even though the server actually tries to do it - I have the standard 200 OK code in the console that I always got for the redirects on other subsides, but the website is not changed (no loading circle or anything). After the completion, I make a POST ajax request and the survey site gives the standard SurveyJS blank website with "Thank you for completing the survey!" (it doesn't load the website or anything)

Console Flask prompts:

INFO:werkzeug:127.0.0.1 - - [03/Jul/2017 20:06:44] "POST /survey HTTP/1.1" 302 -
INFO:werkzeug:127.0.0.1 - - [03/Jul/2017 20:06:45] "GET / HTTP/1.1" 200 -

What can I do to make the redirect work from the Flask level?

1 Answers1

0

I am currently working on a Flask / Vue.js (JavaScript) project, and I ran a quick test to simulate this situation, and I am almost certain that this is what's happening:

Your Flask code is working properly. The problem is with the SurveyJS website. When the SurveyJS website sends the POST request, it is presumably doing it with JavaScript (rather than as an HTML form submission), and their JavaScript code does not have logic set up to handle a redirect response from the server, and so it's effectively ignoring the response from the server.

This is different than when you have a plain-HTML website with a form on it: in that case, my rough understanding is that the browser sends the POST request and handles the response from the server, and it properly handles redirect responses.

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
  • Many thanks for that, it really clarified this idea for me. I think I got the effect that I wanted. In my sendDataToServey(survey) function I just include `window.location.href = "/"` **besides** the Ajax. This directly gives me the redirection I wanted and the POST does its job on silently on the server side; – Mateusz Makowski Jul 07 '17 at 13:53