濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Python的flask接收前臺(tái)的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法

Python的flask接收前臺(tái)的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法

熱門(mén)標(biāo)簽:京華圖書(shū)館地圖標(biāo)注 看懂地圖標(biāo)注方法 蘇州人工外呼系統(tǒng)軟件 打印谷歌地圖標(biāo)注 淮安呼叫中心外呼系統(tǒng)如何 電話機(jī)器人貸款詐騙 廣東旅游地圖標(biāo)注 佛山通用400電話申請(qǐng) 電話外呼系統(tǒng)招商代理

ajax向后臺(tái)發(fā)送數(shù)據(jù):

①post方式

ajax:

@app.route("/find_worldByName",methods=['POST'])
type:'post',
data:{'cname':cname,'continent':continent},
這是post方式傳值
那么在后臺(tái)接收就是:(使用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),所以在后臺(tái)接收的時(shí)候也會(huì)不同。
使用request.form.get 方式獲取的是一個(gè)json字符串(在這個(gè)方法會(huì)自動(dòng)轉(zhuǎn)化json對(duì)象,可以直接用key訪問(wèn))
使用request.values.get 方式獲取的是通過(guò)url傳遞的get參數(shù)

下面的代碼是整個(gè)流程實(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ù):   "+"國(guó)家:"+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;itable_data.length;i++)
                {
                // console.log(table_data[i]);
                }
            var appendHTML = "";
        if($(".map-table tbody tr").length>0){
            $(".map-table tbody tr").remove();
        }
        // alert("list長(zhǎng)度:"+table_data.length)
        for(var i=0; itable_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);
            }
        }
    })
}

前臺(tái)html:

table align="center" style="margin:3px"  cellspacing="7px">
                form id="find_value">
                    label>font color="#ff7f50">輸入國(guó)家:/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>時(shí)間/th>
                th>國(guó)家/th>
                th>累計(jì)確診/th>
                th>累計(jì)治愈/th>
                th>累計(jì)死亡/th>
                th>現(xiàn)存確診/th>
                th>排名/th>
              /tr>
                /thead>
                tbody id="bd_data">
                /tbody>
            /table>

Python flask路由:

@app.route("/find_worldByName")
def find_worldByName():
    #獲取用戶傳來(lái)的數(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("美國(guó)", "")
    # print(res)
    return jsonify({"data": res})

后臺(tái)獲取數(shù)據(jù)庫(kù)數(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接收前臺(tái)的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法的文章就介紹到這了,更多相關(guān)Python flask接收前臺(tái)ajax post和get數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python爬取Ajax動(dòng)態(tài)加載網(wǎng)頁(yè)過(guò)程解析
  • django ajax發(fā)送post請(qǐng)求的兩種方法
  • Python3爬蟲(chóng)中關(guān)于Ajax分析方法的總結(jié)
  • python爬蟲(chóng) 基于requests模塊發(fā)起ajax的get請(qǐng)求實(shí)現(xiàn)解析
  • Python爬取肯德基官網(wǎng)ajax的post請(qǐng)求實(shí)現(xiàn)過(guò)程

標(biāo)簽:中山 湖州 江蘇 畢節(jié) 呼和浩特 駐馬店 衡水 股票

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python的flask接收前臺(tái)的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法》,本文關(guān)鍵詞  Python,的,flask,接收,前臺(tái),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python的flask接收前臺(tái)的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Python的flask接收前臺(tái)的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    周口市| 郧西县| 孟连| 伊春市| 商水县| 霍林郭勒市| 横峰县| 贵溪市| 秀山| 恩平市| 亳州市| 焉耆| 赫章县| 乐陵市| 东乌珠穆沁旗| 平遥县| 襄城县| 宜良县| 同心县| 吕梁市| 松溪县| 韶关市| 博罗县| 陆川县| 玉林市| 临朐县| 江山市| 山东省| 图片| 张家口市| 台中县| 龙里县| 南靖县| 成安县| 徐州市| 临沧市| 嫩江县| 广安市| 包头市| 江源县| 肇东市|