简体   繁体   English

如何以形式获取参数

[英]How to get param in form

My html 我的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? 单击jquery发送的单击按钮时能否获取param upload_file?

My jquery does not work: 我的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 if empty formData如果为空

Read through this : https://www.mattlunn.me.uk/blog/2012/05/sending-formdata-with-jquery-ajax/ 阅读全文: 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 jQuery默认尝试将其设置为字符串,您需要关闭此功能并设置内容类型

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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