input type=file not working with $.ajax?

后端 未结 1 821
忘掉有多难
忘掉有多难 2020-12-07 04:39

I have a tag inside the form which generates a HTML . When I submit the form via form submission (e.g. su

相关标签:
1条回答
  • 2020-12-07 05:08

    It is because jQuery.serialize() serializes only input elements, not the data in them.

    Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

    But it doesn't mean that you can't upload files with ajax. Additional features or plugins might be used to send FormData object.

    You can also use FormData with jQuery if you set the right options:

    var fd = new FormData(document.querySelector("form"));
    fd.append("CustomField", "This is some extra data");
    $.ajax({
      url: "actionClass!actionMethodA.action",
      type: "POST",
      data: fd,
      processData: false,  // tell jQuery not to process the data
      contentType: false   // tell jQuery not to set contentType
    });
    
    0 讨论(0)
提交回复
热议问题