demand:
flask using ajax front end processing request, the request fails interface intervals, the results show the same page
SetTimeout method used, setTimeout (function () {}, 1000); setTimeout the request for the second function can be a
FIG Effect: requested two interfaces, 5 second intervals, show the same page
html:test.html
<html>
<head></head>
<body>
<div class="header">
<h4>标题XXX</h4>
</div>
<div class="body">
<form class="form" method="post">
<br class="form-a"/>
<div class="form-b">
<label for="aaa"> XXX: <input class="form-control1" type="a" id="a"
name="a" maxlength="11"
placeholder="请输入XXX"/> </label>
</div>
<div class="box">
<label> XXXX: </label>
<label class="radio"> <input type="radio" id="b" name="b" value="0" checked=""/><span>b1</span> </label>
<label class="radio"> <input type="radio" id="b" name="b" value="1"/><span>b2</span> </label>
</div>
<br/>
<div class="form">
<button class="btn btn-primary" type="button" id="notify">查询</button>
</div>
</form>
</div>
<div class="c" id="show2" style="display:none">
<div class="header">
<h4>结果:</h4>
</div>
<div class="body" id="show3">
</div>
</div>
<div class="body" id="show4">
</div>
</div>
</body>
// static 根据自己实际路径填写
<script type="text/javascript" src="static/js/main.min.js"></script>
<script type="text/javascript" src="static/js/jquery.min.js"></script>
<script type="text/javascript">
// 消息提示
$('#notify').on('click', function () {
// ajax
var b = $("input[name='b']:checked").val();
var a = $("input[name='a']").val();
var data = {
data: JSON.stringify({
'b': b,
'a': a
}), return; Alert ( 'xxxxx not be empty'); IF (a.length = 0) {
}
}
$ .Ajax ({
type: "the POST",
dataType: "JSON",
URL: "/ test1", // call back method test1
Data: Data,
Success: function (Result) {
the console.log (Result);
{
$ ( '# show2') show () // shown here.
$ ( "# show3") HTML (the Result).
the setTimeout (function () {
$ .ajax ({
of the type: "POST",
dataType: "json" ,
url: "/ test2", // method call back test2
data: data,
success: function (result) {
{# console.log('11111')#}
console.log(result);
{
$("#show4").html(result)
}
},
error: function (result) {
{# console.log(result);//#}
{
alert(result);
}
}
});
},5000) // 间隔5秒
}
},
error: function (result) {
{# console.log(result);//#}
{
alert(result);
}
}
});
})
</script>
</html>
flask:
# 页面
@app.route('/test',methods=['GET','POST'])
def test():
return render_template("test/test.html")
# test1
@app.route('/test112',methods=['GET','POST'])
def test112():
data = json.loads(request.form.get('data'))
b = data['b']
a = data['a']
res = b,a
return jsonify(res)
# test2
@app.route('/test2',methods=['GET','POST'])
def test113():
data = json.loads(request.form.get('data'))
b = data['b']
a = data['a']
res = a
return jsonify(res)