-1

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 {

SubZeno
  • 341
  • 3
  • 15

1 Answers1

0

rewritten to use ajax would look something like this.

 $.post'uploadURI', {data: files.input[0]}, function(data, status) {
     //The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request
   }};

Hope this helps. Ajax is a relatively simple to grasp. A quick look at the docs would probably help as well

radlaz
  • 298
  • 4
  • 16