如何获得形式的参数

时间:2017-06-29 03:21:34

标签: javascript jquery

我的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">

就我而言,我不想把按钮发送到表格中。当jquery发送点击按钮时,我可以获得param upload_file吗?

我的jquery不起作用:

$("#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如果为空

1 个答案:

答案 0 :(得分:0)

通读: https://www.mattlunn.me.uk/blog/2012/05/sending-formdata-with-jquery-ajax/

jQuery默认尝试将其设为字符串,您需要关闭此功能并设置内容类型

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