简体   繁体   中英

How to get param in form

My html

    <form action="/csv/load"  enctype="multipart/form-data" id="csv_upload_form" method="post" style="margin: 10px 0;">
        <input type="file" id="upload_file" name="upload_file" />
        <span><input type="submit" value="Upload File" /></span>
    </form>
    ~~
   <input type="button" name="send" id="send" value="send">

In my case, I do not want to put button send in form. Can I get param upload_file when click button send by jquery?

My jquery does not work:

$("#send" ).click(function() {
      var formData = new FormData($('form#csv_upload_form')[0]);
         // formData = $('#csv_upload_form').find(':input').serializeArray() --> formData is empty
            $.ajax({
                  url: "/csv/register",
                  type: "post",
                  dataType: 'json',
                  data: formData,
                  success: function(response) {
                        ..something
                   }
});

formData if empty

Read through this : https://www.mattlunn.me.uk/blog/2012/05/sending-formdata-with-jquery-ajax/

jQuery by default tries to make it a string, you need to turn this feature off and set the content type as well

$.ajax({
    url: "/csv/register",
    type: "post",
    contentType: false,
    processData: false,
    data: formData,
    success: function(response) {
        ..something
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM