无法从前端到后端接收数据

时间:2019-03-10 06:48:42

标签: python html ajax

数据没有从前端发送到后端,即时消息使用Html和ajax作为前端并链接了部分

// signup.html

<!DOCTYPE html>
<html lang="en">
 <head>
     <script src= "../static/jquery-3.3.1.min.js"></script>
     <script src= "../static/sha1.min.js"></script>
     <script src = "../static/sha1.js"></script>
     <script>
     $(function(){
            $('.btnblock').click(function(){
                      var user = $('#inputUsername').val();
                      var pass = sha1($('#inputPassword').val());
                      //alert(pass);
                      //alert(JSON.stringify({username : user, password : pass}))
                      $.ajax({
                                   url : 'http://localhost:5000/api/v1/users',
                                   data : JSON.stringify({username : user, password : pass}),
                                   type : 'POST',
                                   method : 'post',
                                   ContentType : 'text/plain',
                                   dataType: 'json',
                                   success: function(response){
                                                   alert(response);
                                   },
                                   error: function(error){
                                                   alert(error);
                                   }
                      });
              });
     });
     </script>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <meta name="description" content="">
     <meta name="author" content="">
     <center>
        <h1> Add user. </h1>
    </center>
</head>

<body background="{{ url_for('static', filename='home/background.jpeg')}}">
    <div class="container">
        <form class="form-signin" enctype="multipart/form-data" role="form">
        <input type="text" id="inputUsername" name="username" class="form-control" placeholder="Username" required autofocus>
        <br>
        <br>
        <input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required>
        <br>
        <br>
        <button class="btnblock" type="submit">Register</button>
        </form>
     </div>
</body>

</html>

和处理来自前端的这些请求的代码是:

def addUser():
    if(request.method == 'POST'):
        ##print(request._dict_)
        print("Receiving data....")
        data = request.data.decode()
        print(data)

这里的问题是前端中添加的凭证和数据没有在后端中得到处理,print(request.data)返回空并且每次都不返回添加的数据,我确实尝试过对其进行了几次调试,并搜索了错误内容,但仍然没有任何进展,谢谢您的帮助

0 个答案:

没有答案