Skip to content

Instantly share code, notes, and snippets.

@sashabeep
Created July 24, 2020 14:56
Show Gist options
  • Save sashabeep/420027696f81d0dce4a702a09071f91f to your computer and use it in GitHub Desktop.
Save sashabeep/420027696f81d0dce4a702a09071f91f to your computer and use it in GitHub Desktop.
Send form via ajax with attachments
$(document).on('submit', 'form.myFormWithFiles', function (event) {
event.preventDefault();
event.stopImmediatePropagation();
var $this = $(this);
var formData = new FormData($($this)[0]);
$.ajax({
type: $this.attr('method'),
url: $this.attr('action'),
data: formData,
processData: false,
contentType: false,
dataType: "html",
success: function (response) {
//any success behavior - update popup, etc
$this.get(0).reset();
var response = response;
$("#response").remove();
$('body').append('<div id="response" style="display:none">');
$("#response").html(response);
$("#response").show();
}
});
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment