0
votes

I am having problems when using redirect(url_for('name')) when using javascript and json to obtain the data of the form.

I put prints inside the if-else to see if it was entering and it prints it, but does not redirect.

This is my python code

@app.route('/login/enter', methods=['POST', 'GET'])
def login_enter():
   error = False
   try:
       username = request.get_json()['username']
       password = request.get_json()['password']
       user = User.query.filter_by(username=username).first()

       if user.password != password:
           error = True
   except Exception as e:
       error = True
       print(e)
       db.session.rollback()
   finally:
       db.session.close()

   if error:
       print("Error")
       abort(500)
   else:
       print("No error")
       return redirect(url_for('simulator'))

This is my html code with js

<html>
   <head>
       <title>
           SACPU - Iniciar Sesión
       </title>
       <link rel="shortcut icon" type="image/png" href="static/img/favicon.ico">
       <link rel="stylesheet" href="static/css/style.css">
   </head>
   <body class="fondo">
       <div>
           <form id="form">
               <input type="text" id="username" name="username"/>
               <input type="password" id="password" name="password"/>
               <input type="submit" value="Create"/>
           </form>
       </div>
   </body>

   <script>
       const usernameInput = document.getElementById("username");
       const passwordInput = document.getElementById("password");
       document.getElementById("form").onsubmit = function(e) {

           e.preventDefault();
           const username = usernameInput.value;
           const password = passwordInput.value;
           fetch('/login/enter', {
               method: 'POST',
               body: JSON.stringify({
                   'username': username,
                   'password': password
               }),
               headers: {
                   'Content-Type': 'application/json'
               }
           }).then(
               response => response.json()
           ).then(function(jsonResponse){

           }).catch(function() {

           });
       }

   </script>
</html>

When I use post in the form it redirects correctly, I have tried many solutions but I haven't been able to made it work like I want

This is my python code when using post:

@app.route('/login/enter', methods=['POST', 'GET'])
def login_enter():
   error = False
   try:
       username = request.form.get('username', '')
       password = request.form.get('password', '')
       user = User.query.filter_by(username=username).first()

       if user.password != password:
           error = True
   except Exception as e:
       error = True
       print(e)
       db.session.rollback()
   finally:
       db.session.close()

   if error:
       print("Error")
       abort(500)
   else:
       print("No error")
       return redirect(url_for('simulator'))

This is my html code when using post:

<html>
   <head>
       <title>
           SACPU - Iniciar Sesión
       </title>
       <link rel="shortcut icon" type="image/png" href="static/img/favicon.ico">
       <link rel="stylesheet" href="static/css/style.css">
   </head>
   <body class="fondo">
       <div>
           <form id="form" action="login/enter" method="POST">
               <input type="text" id="username" name="username"/>
               <input type="password" id="password" name="password"/>
               <input type="submit" value="Create"/>
           </form>
       </div>
   </body>
</html>
a redirect in an Ajax response isn't going to actually redirect on the client - you should return some actual JSON (or HTML), have the front end check what that is and then redirect on the client side (eg by setting window.location) if appropriateRobin Zigmond
@RobinZigmond thank you very much, I used window.location and it worked.Susana Chang