ajax上传表单带附件

来源:互联网 发布:吉他班一般多少钱知乎 编辑:程序博客网 时间:2024/06/07 22:38
//FormData是html5的接口,使用它一行代码便可以拿到整个form表单对象:var form = new FormData(document.getElementById("form"));
<!DOCTYPE html>  <html>  <head>      <script src="js/jquery-1.9.1.min.js"></script>      <meta charset="utf-8" />      <title>Ajax提交form</title>      <script type="text/javascript">      function test(){        var form = new FormData(document.getElementById("form"));        $.ajax({        url:"接口地址",        type:"post",        data:form,        cache: false,        processData: false,        contentType: false,        success:function(data){              alert("操作成功!");        },        error:function(e){            alert("网络错误,请重试!!");         }        });              }  </script>  </head>  <body>  <form id="form"  enctype="multipart/form-data">      <input type="text" id="endTime" name="endTime" />      <input type="text" id="type" name="type" />  <input type="file" id="fileAttach" name="fileAttach" />  <input type="button" onclick="test()" value="上传" />  </form>  </body>  </html>  

原文(http://blog.csdn.net/qq_33556185/article/details/51086114)

原创粉丝点击