0

I have a JS function in my HTML template which is assigned to a button tap. When the user clicks the button I'm sending a POST request to my Flask app which should make a redirect to another page. My problem is the redirection doesn't produce a real redirect, I mean my browser doesn't load the next page, while in the console I can see the 301 redirect on the network tab.

According to the browser's network tab, the redirection works, but my browser doesn't load the targeted url. This is how I manage the redirection with Flask:

@app.route("/signup/", methods = ['GET', 'POST'])
def signup():    
    if request.method == 'POST':
        session['usess'] = request.json['usess']
        #return redirect(url_for('index'))
        return redirect("/", code=301)
    if session:
        return redirect("/", code=301)
    else:
        return render_template("signup.html")
    # already tried simply
    # return render_template("signup.html")

And this is what happens in the log of my Flask app:

127.0.0.1 - - [20/Dec/2017 15:01:33] "POST /signup/ HTTP/1.1" 301 -
127.0.0.1 - - [20/Dec/2017 15:01:33] "GET / HTTP/1.1" 200 -

Based on these logs it works, however my browser doesn't want to load the '/' page.

And here is the function that is responsible for the session object. The whole process starts here:

btnSignUp.addEventListener('click', e => {

        // ....
        $.ajax({
          type: "POST",
          contentType: "application/json",
          url: "/signup/",
          data: JSON.stringify({usess: 'session_str'})
          });

});

As the log shows the right requests I don't have an idea what can be the problem. I hope somebody with more experience can tell me what should I alter or try.

rihekopo
  • 3,241
  • 4
  • 34
  • 63
  • An ajax request will **never** reload the page. but you can send a hidden form using regular http, or redirect the user using `window.location.href="http://foobar"` – n00dl3 Dec 20 '17 at 14:50
  • Thank you @n00dl3! So it doesn't related to my server side code? I'm confused because I would say if the POST request works my server side code should make the redirection. I don't want to reload the code with ajax, I would like to reload it with the request I sent via ajax to my server. Or the post request can't call the `def signup()` method? – rihekopo Dec 20 '17 at 15:00
  • The request is made and you method is called, you just need to do redirect the user, the suggested duplicate seems to be the way to go. – n00dl3 Dec 20 '17 at 15:07

0 Answers0