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"

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM