今天小编给大家分享一下JQuery异步post上传表单数据的代码怎么写的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

HTML代码:

<formid="form"enctype="multipart/form-data"method="post"><inputtype="file"name="file1"id="file"accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"><br><inputtype="text"name="l1"id=""value="1"><br><inputtype="number"name="l2"id=""value="2"><br><inputtype="checkbox"name="l3"id=""><br><inputtype="submit"value="上传数据"></form>

jquery代码:

$("#form").submit(function(e){e.preventDefault();//阻止表单刷新,也可以函数最后加上returnfalse;varformData=newFormData($("#form")[0]);//formData对象实例化的参数必须为DOM,加上[0]jquery对象转为dom对象$.ajax({url:"http://0.0.0.1/api",/*接口域名地址*/type:'post',data:formData,contentType:false,processData:false,//如果是跨域请求,请加上下面四行//xhrFields:{//withCredentials:true//},//crossDomain:true,success:function(res){console.log(res);//根据返回的JSON格式数据判断数据传输状态,这个看后端返回的啥数据,没有标准。//if(res.data["code"]=="succ"){//alert('成功');//}elseif(res.data["code"]=="err"){//alert('失败');//}else{//console.log(res);//}},error:function(error){console.log(error);}})});

后端Flask数据处理:

@app.route('/api',methods=['GET','POST'])defapi():#foriteminrequest.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":"处理完毕"}returnjsonify(dict)

以上就是“JQuery异步post上传表单数据的代码怎么写”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。