how to do file upload using jquery serialization

前端 未结 8 1561
感动是毒
感动是毒 2020-11-22 16:28

So I have a form and I\'m submitting the form through ajax using jquery serialization function

        serialized = $(Forms).serialize();

        $.ajax({
         


        
8条回答
  •  青春惊慌失措
    2020-11-22 17:21

    $(document).on('click', '#submitBtn', function(e){
    e.preventDefault();
    e.stopImmediatePropagation();
    var form = $("#myForm").closest("form");
    var formData = new FormData(form[0]);
    $.ajax({
        type: "POST",
        data: formData,
        dataType: "json",
        url: form.attr('action'),
        processData: false,
        contentType: false,
        success: function(data) {
             alert('Sucess! Form data posted with file type of input also!');
        }
    )};});
    

    By making use of new FormData() and setting processData: false, contentType:false in ajax call submission of form with file input worked for me

    Using above code I am able to submit form data with file field also through Ajax

提交回复
热议问题