Ajax提交form表單

來源:互聯網
上載者:User

標籤:log   ast   end   inpu   top   ring   檔案   commons   mile   

FormData,就是這個,再也不用寫一堆js了。

var form = new FormData($("#importForm")[0]); //這裡為什麼要有個[0]呢? 我也不知道 瀏覽器控制台會報錯Unexpected end of JSON input,當時這裡卡了好久,大神看到知道的為啥的給說下$.ajax({                    url:"",                    type:"post",                    data:form,                    cache: false,                    processData:false,                    contentType:false,                    success:function(data){                        var boj = JSON.parse(data);                        console.log(boj);                    },                    error:function(e){                        console.log(e);                    }                });

然後就輕鬆傳到後台了

表單裡可以帶檔案,很爽

<form id="importForm" action="" enctype="multipart/form-data" class="form-horizontal">                <div class="control-group">                <div class="controls">                <input type="button" class="btn btn-primary" value="批量下單" onclick="importc()" style="margin-right: 25px;">                <a href="" style="margin-right: 25px;">下載模板</a>                <input type="file" id="file" name="file">                </div>                </div>                </form>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="defaultEncoding" value="UTF-8"></property>        <property name="maxInMemorySize" value="10240000"></property>    </bean>
對了 這裡不能忘了 springMVC要配置一下

Ajax提交form表單

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.