python html post get,Python的flask接收前台的ajax的post数据和get数据

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方法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参数

d5101dba70f2c65e3a17e1fb628947f6.png

f42ed9c8672e46be30149c1140fc754f.png

下面的代码是整个流程实现:

ajax:1 //查询js 2 function find_res(){ 3     var cname; 4     var continent; 5     // $.ajax 6     // ({ 7     //     method:"post",               8     //     url:"http://localhost:8080/PycharmProjects/Cov/templates/world.html?_ijt=q6ulfhihrfp8rqkl8id73svio3", 9     //     success:function(data)10     //     {11     //         //form表单数据的转化,转化成[ { name: , value:   },{ name: , value:   } ]12     //         all=$('#find_value').serializeArray()13     //         // console.log(all['cname'])14     //         console.log(all[0])15     //         cname=all[0]['value']16     //         alert(cname)17     //     }18     // })19     cname=document.getElementById("cname").value20     continent=document.getElementById("continent").value21     console.log(cname+continent)22     // alert("表单数据:   "+"国家:"+cname+ "大洲:"+ continent)23     $.ajax24     ({25             // sync:true,26             url:"/find_worldByName",27             // type:'post',28             data:{'cname':cname,'continent':continent},29             success:function (data)30             {31                 // alert("!!!")32                 table_data=data.data;33                 for(var i=0;i0){39             $(".map-table tbody tr").remove();40         }41         // alert("list长度:"+table_data.length)42         for(var i=0; i

前台html:1 

输入国家:font>label> 4                      5  6                     输入大洲:font>label> 7                      8  9                     10                     11                 form>12                 13                  14                 时间th>15                 国家th>16                 累计确诊th>17                 累计治愈th>18                 累计死亡th>19                 现存确诊th>20                 排名th>21               tr>22                 thead>23                 24                 tbody>25             table>

Python flask路由:1 @app.route("/find_worldByName") 2 def find_worldByName(): 3     #获取用户传来的数据 4     # jsondata = json.loads(request.form.get('jsondata')) 5     res=[] 6     #get方式 7     cname = request.values.get("cname") 8     continent = request.values.get("continent") 9     #post方式10     # continent = request.form.get("continent")11     # cname = request.form.get("cname")12 13     # print(cname+continent)14     res=utils.find_worldByName(cname,continent)15     # res = utils.find_worldByName("美国", "")16     # print(res)17     return jsonify({"data": res})

后台获取数据库数据:1 def find_worldByName(c_name,continent): 2     print(c_name) 3     print(continent) 4     sql = " SELECT * FROM world WHERE  1=1 " 5     if(c_name!=None): 6         sql=sql+"AND ( c_name LIKE '%"+c_name+"%' )" 7     if(continent!=None): 8         sql=sql+" AND ( continent LIKE '%"+continent+"%') " 9     sql=sql+" AND dt=(SELECT dt FROM world order by dt desc limit 1) order by confirm desc "10 11           # "AND continent LIKE '%%%%%s%%%%'" \12           # " order by dt desc " %(c_name,continent)13     # sql_temp = " SELECT * FROM world WHERE c_name LIKE '%"+c_name+"%' "14     res = query(sql)15     list= []16     for i in res:17         # print(i)18         list.append(i)19     return list;20 21 22 def query(sql,*args):23     """24     通用封装查询25     :param sql:26     :param args:27     :return:返回查询结果 ((),())28     """29     conn , cursor= get_conn()30     print(sql)31     cursor.execute(sql)32     res = cursor.fetchall()33     close_conn(conn , cursor)34     return res

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值