0

I am making a simple login logout functionality using pythons flask(For API) and plain html/JS for front end. Upon successful processing of username and password the API returns "login failed" or "login passed". I want to access those returned strings in my JS script do further coding based on that.

Thanks

dbdriver.py

def userlogin(userID,Password):
    sql = ("SELECT clientCompName_002, clientCompPassword_002 FROM `Clients_002`")
    cursor.execute(sql)
    pairs = cursor.fetchall()
    for pair in pairs:
        if pair['clientCompName_002'] ==userID and pair['clientCompPassword_002']==Password:
            return 'login passed'

    return 'login failed'

views.py

@app.route('/login/<userID>/<Password>', methods=['POST'] )
def login(userID,Password):
    log = dbdriver.userlogin(userID,Password)
    return json.dumps(log, indent=4)

below is the JS script in my html file

<script type="text/javascript">

  function myFunction2(){

    uid=document.getElementById('username').value;
    pass=document.getElementById('password').value;

    url='http://127.0.0.1:5000/login/'+uid+'/'+pass
    window.alert(url)
    document.getElementById('login').setAttribute('action', url);


  }

 
  localStorage.setItem("user","4");

</script>

and the form in my html:

<form id="login" action="#" method="POST" onsubmit="myFunction2()">
<img class="mb-4" src="#" alt="" width="72" height="57">
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>

<div class="form-floating">
  <input type="text" class="form-control" id="username" placeholder="name@example.com">
  <label for="floatingInput">Username</label>
</div>
<div class="form-floating">
  <input type="password" class="form-control" id="password" placeholder="Password">
  <label for="floatingPassword">Password</label>
</div>


<button class="w-100 btn btn-lg btn-primary" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted">&copy; 2017–2021</p>
sam
  • 59
  • 1
  • 2
  • 7
  • Does this answer your question? [How to pass json POST data to Web API method as an object?](https://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-an-object) – Elikill58 Jul 23 '21 at 19:34
  • Please consider using fetch: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch – Ajinkya Bawaskar Jul 23 '21 at 19:34

0 Answers0