Python flask ajax 请求后端并且能获得数据,但是随即浏览器又进行了跳转,请问这是为什么 5
@app.route('/search',methods=['POST'])defsearch():usercode=request.form['STAFF_CODE']...
@app.route('/search',methods=[ 'POST'])
def search():
usercode = request.form['STAFF_CODE']
#print(usercode)
str = No_linshi_01.query.filter_by(usercode=usercode).all()
str_list = []
for u in str:
str_dict = {}
str_dict["id"] = u.id
str_dict["usercode"] = u.usercode
str_dict["username"] = u.username
str_list.append(str_dict)
return jsonify(str_list)-----这是后台
<script type="text/javascript">
$(function () {
$("#btn").click(function () {
var STAFF_CODE=$("#hotelSeq").val();
var tbody=window.document.getElementById("tbody-result");
//var $result = $('#result');
$.ajax({
url:"/search",
type:"POST",
data:{"STAFF_CODE":STAFF_CODE},
dataType:"json",
success:function (data) {
if(data.length != 0){
console.log(data.length);
var str="";
for(i in data){
str += "<tr><td>" + data[i].usercode + "</td><td>" + data[i].username + "</td><tr>" ;
};
//$result.html(str);
tbody.innerHTML = str;
}
else{
alert('未搜索到结果,请确认搜索条件!');
}
}
})
});
});
</script>----前端部分
运行后/sucess能展示后台传来的数据,但是立马又跳转到/search 展示json数据,怎样才能不让它跳转 展开
def search():
usercode = request.form['STAFF_CODE']
#print(usercode)
str = No_linshi_01.query.filter_by(usercode=usercode).all()
str_list = []
for u in str:
str_dict = {}
str_dict["id"] = u.id
str_dict["usercode"] = u.usercode
str_dict["username"] = u.username
str_list.append(str_dict)
return jsonify(str_list)-----这是后台
<script type="text/javascript">
$(function () {
$("#btn").click(function () {
var STAFF_CODE=$("#hotelSeq").val();
var tbody=window.document.getElementById("tbody-result");
//var $result = $('#result');
$.ajax({
url:"/search",
type:"POST",
data:{"STAFF_CODE":STAFF_CODE},
dataType:"json",
success:function (data) {
if(data.length != 0){
console.log(data.length);
var str="";
for(i in data){
str += "<tr><td>" + data[i].usercode + "</td><td>" + data[i].username + "</td><tr>" ;
};
//$result.html(str);
tbody.innerHTML = str;
}
else{
alert('未搜索到结果,请确认搜索条件!');
}
}
})
});
});
</script>----前端部分
运行后/sucess能展示后台传来的数据,但是立马又跳转到/search 展示json数据,怎样才能不让它跳转 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询