flask接收前台的ajax的post数据

html

  

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/css/index.css">

<script src="/static/js/jquery.min.js"></script>
</head>
<body>
<input type="text" name="usernam" id="username">
<input type="password" name="password" id="password">
<button id="btn">点击</button>
<script>
$(function(){
$("#btn").click(function(){
var username=$('#username').val();
var password=$('#password').val();
var data= {
data: JSON.stringify({
'username': username,
'password': password
}),
}
$.ajax({
url:'http://localhost:7999/sendAjax2',
type:'POST',
data:data,
dataType: 'json',
success:function(res){
console.log(res)
console.log(0)

},
error:function (res) {
console.log(res);
console.log(1)
}

})
})
})
</script>
</body>
</html>

@app.route('/sendAjax2', methods=['POST'])
def sendAjax2():
    # password = request.form.get('password')
    # username = request.args.get('username')

    data = json.loads(request.form.get('data'))
    username = data['username']
    password = data['username']
    print (username)
    print (password)
    return "46575"

  

posted @ 2017-01-22 15:37  webbky  阅读(19248)  评论(4编辑  收藏  举报