ajax向后台发送数据:
①post方式
ajax:
@app.route("/find_worldbyname",methods=['post'])
type:'post',
data:{'cname':cname,'continent':continent},
这是post方式传值
那么在后台接收就是:(使用request的form方法)
continent = request.form.get("continent")
cname = request.form.get("cname")
②get方式(url参数)
使用request的values方法
使用request的values方法
data:{'cname':cname,'continent':continent},
name=request.values.get("cname")
总结:
这两种的区别就是数据在ajax data里的发送方式不同(get和post),所以在后台接收的时候也会不同。
使用request.form.get 方式获取的是一个json字符串(在这个方法会自动转化json对象,可以直接用key访问)
使用request.values.get 方式获取的是通过url传递的get参数


下面的代码是整个流程实现:ajax:
//查询js
function find_res(){
var cname;
var continent;
// $.ajax
// ({
// method:"post",
// url:"http://localhost:8080/pycharmprojects/cov/templates/world.html?_ijt=q6ulfhihrfp8rqkl8id73svio3",
// success:function(data)
// {
// //form表单数据的转化,转化成[ { name: , value: },{ name: , value: } ]
// all=$('#find_value').serializearray()
// // console.log(all['cname'])
// console.log(all[0])
// cname=all[0]['value']
// alert(cname)
// }
// })
cname=document.getelementbyid("cname").value
continent=document.getelementbyid("continent").value
console.log(cname continent)
// alert("表单数据: " "国家:" cname "大洲:" continent)
$.ajax
({
// sync:true,
url:"/find_worldbyname",
// type:'post',
data:{'cname':cname,'continent':continent},
success:function (data)
{
// alert("!!!")
table_data=data.data;
for(var i=0;i0){
$(".map-table tbody tr").remove();
}
// alert("list长度:" table_data.length)
for(var i=0; i"
table_data[i][2] ""
table_data[i][5] ""
table_data[i][8] ""
table_data[i][9] ""
table_data[i][4] ""
(i 1) "";
$(".map-table tbody").append(appendhtml);
}
}
})
}
前台html:
| 时间 | 国家 | 累计确诊 | 累计治愈 | 累计死亡 | 现存确诊 | 排名 |
|---|
python flask路由:
@app.route("/find_worldbyname")
def find_worldbyname():
#获取用户传来的数据
# jsondata = json.loads(request.form.get('jsondata'))
res=[]
#get方式
cname = request.values.get("cname")
continent = request.values.get("continent")
#post方式
# continent = request.form.get("continent")
# cname = request.form.get("cname")
# print(cname continent)
res=utils.find_worldbyname(cname,continent)
# res = utils.find_worldbyname("美国", "")
# print(res)
return jsonify({"data": res})
后台获取数据库数据:
def find_worldbyname(c_name,continent):
print(c_name)
print(continent)
sql = " select * from world where 1=1 "
if(c_name!=none):
sql=sql "and ( c_name like '%" c_name "%' )"
if(continent!=none):
sql=sql " and ( continent like '%" continent "%') "
sql=sql " and dt=(select dt from world order by dt desc limit 1) order by confirm desc "
# "and continent like '%%%%%s%%%%'" \
# " order by dt desc " %(c_name,continent)
# sql_temp = " select * from world where c_name like '%" c_name "%' "
res = query(sql)
list= []
for i in res:
# print(i)
list.append(i)
return list;
def query(sql,*args):
"""
通用封装查询
:param sql:
:param args:
:return:返回查询结果 ((),())
"""
conn , cursor= get_conn()
print(sql)
cursor.execute(sql)
res = cursor.fetchall()
close_conn(conn , cursor)
return res
到此这篇关于python的flask接收前台的ajax的post数据和get数据的方法的文章就介绍到这了,更多相关python flask接收前台ajax post和get数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
adamsir