1. 程式人生 > >JS 將form表單與上傳檔案 一起提交

JS 將form表單與上傳檔案 一起提交

前提 需要引入jquery(不然無法使用$.ajax)

//js
function add(){
    var formData = new FormData(document.querySelector("form"));//獲取form值
        $.ajax({
            url: "http://chetest.svell.cn/jfinal_demo/student/test",
            type: "POST",
            data: formData,
            processData: false,  // 不處理資料
            contentType: false
, // 不設定內容型別 success:function(data){ alert(data); } }); }
//html
<form>
    <input type="text" name="name">
    <input type="text" name="age">
    <input type="text" name="phone">
    <input type="file" name="file">
    <input type
=
"button" onclick="add()" value="新增"> </form>