JQuery異步post上傳表單數(shù)據(jù)標(biāo)準(zhǔn)化模板
HTML代碼:
<form id="form" ?enctype="multipart/form-data" method="post" > ?? ?<input type="file" name="file1" id="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"><br> ?? ?<input type="text" name="l1" id="" value="1"><br> ?? ?<input type="number" name="l2" id="" value="2"><br> ?? ?<input type="checkbox" name="l3" id="" ><br> ?? ?<input type="submit" value="上傳數(shù)據(jù)"> </form>
jquery代碼:
$("#form").submit(function (e) {?
? ? e.preventDefault();//阻止表單刷新,也可以函數(shù)最后加上 return false;
? ? var formData = new FormData($("#form")[0]);//formData對(duì)象實(shí)例化的參數(shù)必須為DOM,加上[0]jquery對(duì)象轉(zhuǎn)為dom對(duì)象
? ? $.ajax({
? ? ? ? url:"http://0.0.0.1/api", /*接口域名地址*/
? ? ? ? type:'post',
? ? ? ? data: formData,
? ? ? ? contentType: false,
? ? ? ? processData: false,
? ? ? ? //如果是跨域請(qǐng)求,請(qǐng)加上下面四行
? ? ? ? //xhrFields: {
? ? ? ? // ? ?withCredentials: true
? ? ? ? //},
? ? ? ? //crossDomain: true,
? ? ? ? success:function(res){
? ? ? ? ? ? console.log(res);
? ? ? ? ? ? //根據(jù)返回的JSON格式數(shù)據(jù)判斷數(shù)據(jù)傳輸狀態(tài),這個(gè)看后端返回的啥數(shù)據(jù),沒有標(biāo)準(zhǔn)。
? ? ? ? ? ? // if(res.data["code"]=="succ"){
? ? ? ? ? ? // ? ? alert('成功');
? ? ? ? ? ? // }else if(res.data["code"]=="err"){
? ? ? ? ? ? // ? ? alert('失敗');
? ? ? ? ? ? // }else{
? ? ? ? ? ? // ? ? console.log(res);
? ? ? ? ? ? // }
? ? ? ? },
? ? ? ? error:function(error){
? ? ? ? ? ? console.log(error);
? ? ? ? }
? ? })
});后端Flask數(shù)據(jù)處理:
@app.route('/api',methods=['GET','POST'])?
def api():
? ? # for item in request.form:
? ? # ? ? print(item)
? ? d1 = request.form.get("l1")
? ? d2 = request.form.get("l2")
? ? d3 = request.form.get("l3")
? ? file = request.files.get('file')?
? ? print(file)
??? ?dict = {"code":"200","data":"處理完畢"}
? ? return jsonify(dict)到此這篇關(guān)于JQuery異步post上傳表單數(shù)據(jù)標(biāo)準(zhǔn)化模板的文章就介紹到這了,更多相關(guān)JQuery異步post上傳表單數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jQuery 1.9移除了$.browser可以使用$.support來(lái)替代
這篇文章主要介紹jQuery 1.9 移除了 $.browser 的替代方法,需要的朋友可以參考下2014-09-09
jQuery實(shí)現(xiàn)滑動(dòng)星星評(píng)分效果(每日分享)
jQuery星星評(píng)分制作5顆星星鼠標(biāo)滑過(guò)評(píng)分打分效果,可取消評(píng)分結(jié)果,重新打分。下面通過(guò)代碼給大家講解的非常詳細(xì),需要的的朋友參考下2019-11-11
jQuery焦點(diǎn)圖輪播插件KinSlideshow用法分析
這篇文章主要介紹了jQuery焦點(diǎn)圖輪播插件KinSlideshow用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了jQuery焦點(diǎn)圖輪播插件KinSlideshow的參數(shù)含義與使用方法,需要的朋友可以參考下2016-06-06
jQuery+CSS3文字跑馬燈特效的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇jQuery+CSS3文字跑馬燈特效的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家看,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
Jquery實(shí)現(xiàn)兼容各大瀏覽器的Enter回車切換輸入焦點(diǎn)的方法
本文主要講解的是在做項(xiàng)目中遇到需要Enter回車切換輸入焦點(diǎn)的解決思路以及最終解決方案,很簡(jiǎn)單,需要的朋友可以參考下2014-09-09
Jquery方式獲取iframe頁(yè)面中的 Dom元素
這篇文章主要介紹了Jquery方式如何獲取iframe頁(yè)面中的Dom元素,需要的朋友可以參考下2014-05-05
jQuery中(function($){})(jQuery)詳解
本文通過(guò)具體示例向大家詳細(xì)介紹了jQuery中的(function($){})(jQuery)的用法和意義,對(duì)此有相同困惑的小伙伴可以參考下本文。2015-07-07
jQuery實(shí)現(xiàn)帶有動(dòng)畫效果的回到頂部和底部代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)帶有動(dòng)畫效果的回到頂部和底部代碼,涉及jQuery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
jquery實(shí)現(xiàn)按Enter鍵觸發(fā)事件示例
按Enter鍵觸發(fā)事件比如提交等等,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-09-09

