I use this script to upload a file via browser:
function onSubmit() {
const url = 'uploadURl';
fetch(url, {
method: 'POST',
body: input.files[0]
}).then(function(res) {
console.log(res);
location.reload();
})
.catch(function(err) {
console.error('Got error:', err);
});
}
It works fine, but I would like to use $.ajax
instead of fetch
.
How can it be rewritten? In particular, I still did not get how to properly setup the request body for the file.
Update:
If I write:
$.post({
url: 'uploadUrl',
{ data: input.files[0] }
})
.done(function() {
console.log('File uploaded');
});
I get: Uncaught SyntaxError: Unexpected token {