如何实现前端输入框输入用户名,并使用ajax与flask实时在输入框后显示此用户名?
我的代码如下:
html部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>echarts</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script>
<script type = "text/javascript"
src = "{{ url_for('static', filename = 'index.js') }}" ></script>
</head>
<body>
<div id="container"></div>
<div >数据展示:<input type="text" id="name1"></div>
<div id="info"></div>
</body>
</html>
ajax代码:
$(document).ready(function(){
$("#name").keyup(function(){
var a=document.getElementById(name1).innerText
$.get("/hello",a,function(data){
$('#info').html(data)
result = data
})
});
})
flask代码:
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, render_template,request
import json
app = Flask(__name__,template_folder='template') # 实例化app对象
@app.route('/hello', methods=['GET', 'POST']) # 路由
def test_post():
a=request.form()
return a
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/index')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='127.0.0.1', # 任何ip都可以访问
port=7777, # 端口
debug=True
)