2017-02-20 48 views
-1

我正在用户注册按钮构建一个应用程序,并希望检查它是否正常工作,但单击按钮后没有返回值。无法从Flask用户中的jQuery发布数据登录按钮

enter image description here

我当用户点击注册按钮添加一个jQuery POST请求。 这里是btnSignUpsignup.js下\静\ JS文件夹:

$(function(){ 
     $('#btnSignUp').click(function(){ 

    $.ajax({ 
     url: "/signUp", 
     data: $('form').serialize(), 
     type: "POST", 
     success: function(response){ 
      console.log(response); 
     }, 
     error: function(error){ 
      console.log(error); 
     } 
     }); 
    }); 
}); 

然后简单地检查它们是否有效,暂且让我们只返回一个简单的消息,这里是在app.py代码:

@app.route('/signUp',methods=['POST']) 
def signUp(): 

    # read the posted values from the UI 
    _name = request.form['inputName'] 
    _email = request.form['inputEmail'] 
    _password = request.form['inputPassword'] 

    # validate the received values 
    if _name and _email and _password: 
     return json.dumps({'html':'<span>All fields good !!</span>'}) 
    else: 
     return json.dumps({'html':'<span>Enter the required fields</span>'}) 

最后是符号界面,signup.html

<h1>Register</h1> 
    <form class="form-signin"> 
    <label for="inputName" class="sr-only">Name</label> 
    <input type="name" name="inputName" id="inputName" class="form-control" placeholder="Name" required autofocus> 
    <label for="inputEmail" class="sr-only">Email address</label> 
    <input type="email" name="inputEmail" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> 
    <label for="inputPassword" class="sr-only">Password</label> 
    <input type="password" name="inputPassword" id="inputPassword" class="form-control" placeholder="Password" required> 

    <button id="btnSignUp" class="btn btn-lg btn-primary btn-block" type="button">Sign up</button> </form> 

我也检查如果jQuery是正确下载并放置:

enter image description here

而下面是我的Python控制台,不存在“POST”点击注册后,键多次:

enter image description here

请告知,如果任何有任何问题,谢谢。

回答

0

signup.html中加入这两行代码后就解决了这个问题。

<script src="/static/js/jquery-3.1.1.js"></script> 
<script src="/static/js/signup.js"></script>