How to pass file data with AJAX and jQuery?

前端 未结 4 904
心在旅途
心在旅途 2020-12-11 05:18

I\'m trying to create a form that allows a user to fill out data and if an option is checked a div opens up and the user has the option to upload a file along with their sub

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 06:21

    you can do this using FormData. try this

    $("form#data").submit(function() {
    
        var formData = new FormData($(this)[0]);
    
        $.post($(this).attr("action"), formData, function(data) {
            alert(data);
        });
    
        return false;
    });
    
    // HTML
    

提交回复
热议问题