简体   繁体   English

jQuery .get()不向服务器发送请求

[英]jQuery .get() not sending request to server

application.py: application.py:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def index():

    return render_template("index.html")

@app.route("/check", methods=["GET", "POST"])
def check():

    print("receiving a request")

    return "False"

index.html index.html

<!DOCTYPE html>

<title>Testing jQuery</title>

<html>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <body>
        <div class="container">
            <form action="/register" method="GET">
                <div class="form-group">
                    <input type="text" autofocus placeholder="Username" name="username" id="user-name">
                </div>
                <div class="form-group">
                    <input type="text" placeholder="password" name="test1">
                </div>
                <button type="submit" class="btn btn-success">Register</button>
            </form>
        </div>

        <script>
            $(document).ready(function() {
                $("#user-name").blur(function() {
                    alert("One")

                    // var xhttp = new XMLHttpRequest();
                    // xhttp.open("GET", "/check", true);
                    // xhttp.send();

                    $.get("/check")
                });
            });
        </script>
    </body>
</html>

My problem is located inside the script tag. 我的问题位于script标记内。

When I am clicking on the password textbox to trigger the blur function, the alert will work but no request being made from jQuery .get(). 当我单击密码文本框以触发模糊功能时,警报将起作用,但不会从jQuery .get()发出请求。 Whereas the Ajax code that is commented out, will be able to call the check function. 而注释掉的Ajax代码将能够调用check函数。

So I'm wondering what is it am I missing to be able to use .get()? 所以我想知道能够使用.get()是什么?

Thank you for reading. 感谢您的阅读。

When I run code it shows in console in browser $.get is not a function . 当我运行代码时,它显示在浏览器的控制台$.get is not a function

When I used Google to find this text I get that query slim doesn't have this function and you have to use normal version, not slim. 当我使用Google查找此文本时,我得到的query slim没有此功能,您必须使用普通版本,而不是slim。

<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>

After using this version I can see receiving a request 使用此版本后,我可以看到receiving a request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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