flask接收前台的ajax的数据
Posted webbky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask接收前台的ajax的数据相关的知识,希望对你有一定的参考价值。
<!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"
以上是关于flask接收前台的ajax的数据的主要内容,如果未能解决你的问题,请参考以下文章