So I'm currently working in the /check route and have tried to make it work to no avail. I'm pretty sure the error lies in my JQuery but I don't know how to fix it. Any help is aprreciated.
register.html:
{% extends "layout.html" %}
{% block tite %}
Register
{% endblock %}
{% block main %}
<form action="/register" method="post" id="form">
<div class="form-group">
<input name="username" placeholder="Username" id="username" autocomplete="off" autofocus class="form-control" type="text" required>
</div>
<div class="form-group">
<input class="form-control" name="password" placeholder="Password" type="password" required>
</div>
<div class="form-group">
<input class="form-control" name="confirmation" placeholder="Confirm password" type="password" irequired>
</div>
<button class="btn btn-primary" type="submit" id="register">Register</button>
<script>
$(document.querySelector("#register").on('click', function( event ) {
event.preventDefault();
$.get("/check", {username: document.querySelector("#username")}, function ( resp ) {
returned = console.log( resp );
})
if (returned.value == false) {
alert("Username is already taken!");
}
else {
document.querySelector("#form").submit();
};
});
</script>
</form>
{% endblock %}
/check route:
@app.route("/check", methods=["GET"])
def check():
"""Return true if username available, else false, in JSON format"""
username = request.args.get("username")
current_usernames = db.execute("SELECT username FROM users")
for check_username in current_usernames:
if username == check_username:
return jsonify("true")
return jsonify("false")