Issues with FormData Being Null?

前端 未结 2 1991
太阳男子
太阳男子 2021-01-25 12:49

I\'m seeming to have an issue with FormData being null. I\'m trying to upload files and JSON in a single POST request. I\'ve tried a variety of things, but nothing has seemed to

相关标签:
2条回答
  • 2021-01-25 13:18

    Okay I have same problem and I think know the solution after I read this documentation from MDN.

    I fix it with this way:

    var formData = new FormData();
    // After instantiate the object we should use append method
    // And append more data as you needed with this way:
    
    formData.append('prices', $("#columnPrices").val());
    formData.append('id', $("#columnIds").val());
    formData.append('file', $("#uploadCSVWithData");
    
    $(this).html('<i class="fa fa-spinner fa-spin" aria-hidden="true">');
    $.ajax({
        type: "POST",
        url: '{removed}',
        data: formData,
        success: function(done) {
            $("#submitCSVForUpload").html("Submit");
            $("#uploadFromCSVModal").modal('hide');
        },
        processData: false,
        contentType: false
    });
    

    I hope it can help you :D

    0 讨论(0)
  • 2021-01-25 13:31

    FormData can't be inspected with console.log without iterating.

    Here is an article on how to inspect FormData

    If you want to console.log your FormData object before doing the ajax post request, you can do something like this (I'm using the example from the source docs with your code):

    var form = $('#uploadCSVWithData')[0];
    var data = new FormData(form);
    
    // Display the key/value pairs
    for(var pair of data.entries()) {
       console.log(pair[0]+ ', '+ pair[1]); 
    }
    
    0 讨论(0)
提交回复
热议问题