简体   繁体   中英

request.args.get() returns None type value (None)

I am trying to check the users input on my login form. I am sending an HTTP request to the server to check the database for the username. Here is the network URL: https://bceec5a5-eba3-49e3-b255-d3976d185fad-ide.cs50.xyz:8080/user_name?username=fabianomobono

Here's the html

<form id="login_form" action='/home' method='post'>
        <input id="login_credentials_username" name='login_username' type='text' placeholder='Username' >
        <input id="login_credentials_password" name='login_password' type='password' placeholder="Password" >
        <button class="btn btn-primary" type='submit'>Log in</button>
      </form>

Here's the JS:

$('#login_form').ready(function() {

    $('#login_form').on('submit', function(e) {

        e.preventDefault();
        logincheck();

    });
});

    function logincheck(){
        var username = document.getElementById("login_credentials_username").value;
        var password = document.getElementById("login_credentials_password").value;

        if (username == ''){
          alert("no user");
          return false;
        }
        else if (password == ''){
           alert('no password');
           return false;
        }

        else if (password && username){
            alert(password + username);
            console.log(username)
            $.get('/user_name?username=' + username, function(r){
                if (r === false){
                    alert('python returned false');
                    return false;
                }

                else{
                    alert('python returned true');
                    return true;
                }

            });
            return false;

        }

      else {

        return true;
      }

         }

Here's python on the server side:

@app.route("/user_name", methods=["GET"])
def login_usercheck():
    print(Fore.GREEN + "user_check function, line 171")
    username = (request.args.get('login_username'),)
    print(username)
    conn = sqlite3.connect('database.db')
    c = conn.cursor()
    c.execute("SELECT username FROM users WHERE username =?", username)
    old_user = c.fetchall()

    if len(old_user) > 0:
        return jsonify(True)

    else:
        return jsonify(False)

The problem is that my username variable in the python function always returns NULL. I tried all combinations of,(request.form.get, request.args.get... and so on) Funny thing is I have a similar function to check for the register credentials and that one works just fine. I can't seem to figure out what the problem is... Here's what I get in the terminal:

(None,) 192.168.164.98 - - [05/Nov/2019 17:54:01] "GET /user_name?username=cf HTTP/1.0" 200 -

Possibly a Typo?
In your Javascript you have:

$.get('/user_name?username=' + username, function(r){

In your Python you have:

username = (request.args.get('login_username'),)

Try request.args.get('username')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM