我正在尝试检查登录表单上的用户输入。我正在向服务器发送HTTP请求,以检查数据库中的用户名。这是网络网址:https://bceec5a5-eba3-49e3-b255-d3976d185fad-ide.cs50.xyz:8080/user_name?username=fabianomobono
这是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>
这是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;
}
}
这是服务器端的python:
@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)
问题是我在python函数中的用户名变量始终返回NULL。我尝试了(request.form.get,request.args.get ...等的所有组合)有趣的是,我有一个类似的功能来检查注册凭证,并且它工作得很好。我似乎无法弄清楚问题出在哪里...这是我在终端机中得到的:
(无,)192.168.164.98--[05 / Nov / 2019 17:54:01]“ GET / user_name?username = cf HTTP / 1.0” 200-
答案 0 :(得分:1)
可能是拼写错误?
在您的Javascript中,您可以:
$.get('/user_name?username=' + username, function(r){
在Python中,您可以:
username = (request.args.get('login_username'),)
尝试request.args.get('username')