首页 > 代码库 > flask接收前台的ajax的数据

flask接收前台的ajax的数据

html

  

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

<script src="http://www.mamicode.com/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= http://www.mamicode.com/{
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 = http://www.mamicode.com/json.loads(request.form.get(‘data‘))"46575"

  

flask接收前台的ajax的数据