Python的flask接收前臺的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法
ajax向后臺發(fā)送數(shù)據(jù):
①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參數(shù))
使用request的values方法
使用request的values方法
data:{'cname':cname,'continent':continent},
name=request.values.get("cname")
總結(jié):
這兩種的區(qū)別就是數(shù)據(jù)在ajax data里的發(fā)送方式不同(get和post),所以在后臺接收的時候也會不同。
使用request.form.get 方式獲取的是一個json字符串(在這個方法會自動轉(zhuǎn)化json對象,可以直接用key訪問)
使用request.values.get 方式獲取的是通過url傳遞的get參數(shù)


下面的代碼是整個流程實現(xiàn):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表單數(shù)據(jù)的轉(zhuǎn)化,轉(zhuǎn)化成[ { 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("表單數(shù)據(jù): "+"國家:"+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;i<table_data.length;i++)
{
// console.log(table_data[i]);
}
var appendHTML = "";
if($(".map-table tbody tr").length>0){
$(".map-table tbody tr").remove();
}
// alert("list長度:"+table_data.length)
for(var i=0; i<table_data.length; i++)
{
//分割日期字符串
strdt=table_data[i][1].split(" ");
strdt=strdt[0]+strdt[1]+strdt[2]+strdt[3]
appendHTML = "<tr align='center' style='color:aquamarine;'><td>"+
strdt+"</td><td>"+
table_data[i][2]+"</td><td>"+
table_data[i][5]+"</td><td>"+
table_data[i][8]+"</td><td>"+
table_data[i][9]+"</td><td>"+
table_data[i][4]+"</td><td>"+
(i+1)+"</td></tr>";
$(".map-table tbody").append(appendHTML);
}
}
})
}
前臺html:
<table align="center" style="margin:3px" cellspacing="7px">
<form id="find_value">
<label><font color="#ff7f50">輸入國家:</font></label>
<input id="cname" type="text" name="cname" placeholder="" value="">
<label><font color="#ff7f50">輸入大洲:</font></label>
<input id="continent" type="text" name="continent" placeholder="" value="">
<input type="button" value="查詢" onclick="find_res()">
<input type="reset" value="重置">
</form>
<thead>
<tr style="color: #FFB6C1">
<th>時間</th>
<th>國家</th>
<th>累計確診</th>
<th>累計治愈</th>
<th>累計死亡</th>
<th>現(xiàn)存確診</th>
<th>排名</th>
</tr>
</thead>
<tbody id="bd_data">
</tbody>
</table>
Python flask路由:
@app.route("/find_worldByName")
def find_worldByName():
#獲取用戶傳來的數(shù)據(jù)
# 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})
后臺獲取數(shù)據(jù)庫數(shù)據(jù):
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:返回查詢結(jié)果 ((),())
"""
conn , cursor= get_conn()
print(sql)
cursor.execute(sql)
res = cursor.fetchall()
close_conn(conn , cursor)
return res
到此這篇關(guān)于Python的flask接收前臺的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法的文章就介紹到這了,更多相關(guān)Python flask接收前臺ajax post和get數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
提高Python生產(chǎn)力的五個Jupyter notebook插件
Jupyter Notebook 因其可用性和實用性而成為數(shù)據(jù)分析和機器學(xué)習(xí)模型領(lǐng)域最流行的 IDE,它也是很多數(shù)據(jù)初學(xué)者的首選 IDE。它最具特色的是,擁有豐富的插件、擴展數(shù)據(jù)處理能力和提升工作效率2021-11-11
python實現(xiàn)數(shù)據(jù)寫入excel表格
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)數(shù)據(jù)寫入excel表格,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Pandas中DataFrame交換列順序的方法實現(xiàn)
這篇文章主要介紹了Pandas中DataFrame交換列順序的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Django添加bootstrap框架時無法加載靜態(tài)文件的解決方式
這篇文章主要介紹了Django添加bootstrap框架時無法加載靜態(tài)文件的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python機器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor)實例詳解
這篇文章主要介紹了Python機器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor),結(jié)合實例形式分析了k-近鄰算法的原理、操作步驟、相關(guān)實現(xiàn)與使用技巧,需要的朋友可以參考下2018-06-06

