So I have this form, that uploads a file to a third party host, upload works fine, but I'm getting a redirect when upload is done, I have read that Ajax can solve this, but I have tried many Ajax solutions and non seem to have worked, might it be because the action is done outside of my website? any help is much appreciated!
<form id="myform" enctype="multipart/form-data" action="https://website/upload/01" method="post">
<input type="hidden" name="api_key" value="XXXXXXX">
<input name="file" type="file">
<input value="Upload" type="submit">
You can use e.preventDefault();
Here i attached sample code with Jquery and ajax.
jQuery + Ajax
$("form#myform").submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: window.location.pathname, // location of form submit
type: 'POST',
data: formData,
success: function (data) {
alert(data)
},
cache: false,
contentType: false,
processData: false
});
});
Reference: Uploading both data and files in one form using Ajax?