国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Python的flask接收前臺的ajax的post數據和get數據的方法

瀏覽:5日期:2022-06-22 16:14:44

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參數

Python的flask接收前臺的ajax的post數據和get數據的方法

Python的flask接收前臺的ajax的post數據和get數據的方法

下面的代碼是整個流程實現:ajax:

//查詢jsfunction 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;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 cellspacing='7px'><form id='find_value'> <label><font color='#ff7f50'>輸入國家:</font></label> <input type='text' name='cname' placeholder='' value=''> <label><font color='#ff7f50'>輸入大洲:</font></label> <input 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>現存確診</th><th>排名</th> </tr></thead><tbody id='bd_data'></tbody> </table>

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數據內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美另类videosgrstv变态 欧美另类高清xxxxx | 中文国产成人精品久久一区 | 亚洲视频在线观看视频 | 亚洲图片视频在线观看 | 日韩欧美黄色 | 中文字幕在线一区二区三区 | 91久久青青草原免费 | 亚洲欧美天堂 | 三级黄色高清视频 | 草免费视频 | 一道精品视频一区二区三区图片 | 亚洲无限看 | 国产精品国产高清国产专区 | 国产亚洲欧美日韩在线观看一区二区 | 91大神大战丝袜美女在线观看 | 亚洲人成网站观看在线播放 | 免费一级毛片在播放视频 | a级精品九九九大片免费看 a级毛片免费观看网站 | 久久亚洲精品中文字幕亚瑟 | 中国国产一国产一级毛片视频 | 亚洲免费成人 | 一级性片 | 亚洲自拍高清 | 蘑菇午夜三级 | 日韩毛片高清免费 | 91看片淫黄大片欧美看国产片 | 草草影院在线观看 | 中国国产一国产一级毛片视频 | 欧美大尺度免费一级特黄 | 亚洲欧美日韩久久精品第一区 | 成人三级精品视频在线观看 | 国产成人艳妇在线观看 | 最近免费手机中文字幕3 | 日韩freesex呦交 | 国产在线观看午夜不卡 | 日韩成人黄色片 | 久久亚洲精品视频 | 91精品国产免费久久久久久青草 | 99国产精品欧美久久久久久影院 | 久久夜色精品国产噜噜亚洲a | 伊人短视频 |