Skip to main content
Improved v2
Source Link
BlackBeard
  • 10.5k
  • 7
  • 54
  • 63

You can use newer newer Fetch API by JavaScript. Like this:

function uploadButtonCLicked(){
    var input = document.querySelector('input[type="file"]')
    
    fetch('/url', {
      method: 'POST',
      body: input.files[0]
    }).then(res => res.json())   // you can do something with response
      .catch(error => console.error('Error:', error))
      .then(response => console.log('Success:', response));
}                               

Advantage: Fetch API is natively supported by all modern browsers, so you don't have to import anything. Also, note that fetch() returns a Promise which is then handled by using .then(..code to handle response..) asynchronously.

You can use newer Fetch API by JavaScript. Like this:

function uploadButtonCLicked(){
    var input = document.querySelector('input[type="file"]')
    
    fetch('/url', {
      method: 'POST',
      body: input.files[0]
    }).then(res => res.json())   // you can do something with response
      .catch(error => console.error('Error:', error))
      .then(response => console.log('Success:', response));
}

You can use newer Fetch API by JavaScript. Like this:

function uploadButtonCLicked(){
    var input = document.querySelector('input[type="file"]')
    
    fetch('/url', {
      method: 'POST',
      body: input.files[0]
    }).then(res => res.json())   // you can do something with response
      .catch(error => console.error('Error:', error))
      .then(response => console.log('Success:', response));
}                               

Advantage: Fetch API is natively supported by all modern browsers, so you don't have to import anything. Also, note that fetch() returns a Promise which is then handled by using .then(..code to handle response..) asynchronously.

Improved format
Source Link
BlackBeard
  • 10.5k
  • 7
  • 54
  • 63

You can use newer Fetch API by JavaScript. Like this:

function uploadButtonCLicked(){
    var input = document.querySelector('input[type="file"]')
    
    fetch('/url', {
      method: 'POST',
      body: input.files[0]
    }).then(res => res.json())   // you can do something with response
      .catch(error => console.error('Error:', error))
      .then(response => console.log('Success:', response));
}

You can use newer Fetch API by JavaScript. Like this:

function uploadButtonCLicked(){
    var input = document.querySelector('input[type="file"]')
    
    fetch('/url', {
      method: 'POST',
      body: input.files[0]
    }).then(res => res.json())
      .catch(error => console.error('Error:', error))
      .then(response => console.log('Success:', response));
}

You can use newer Fetch API by JavaScript. Like this:

function uploadButtonCLicked(){
    var input = document.querySelector('input[type="file"]')
    
    fetch('/url', {
      method: 'POST',
      body: input.files[0]
    }).then(res => res.json())   // you can do something with response
      .catch(error => console.error('Error:', error))
      .then(response => console.log('Success:', response));
}
Source Link
BlackBeard
  • 10.5k
  • 7
  • 54
  • 63

You can use newer Fetch API by JavaScript. Like this:

function uploadButtonCLicked(){
    var input = document.querySelector('input[type="file"]')
    
    fetch('/url', {
      method: 'POST',
      body: input.files[0]
    }).then(res => res.json())
      .catch(error => console.error('Error:', error))
      .then(response => console.log('Success:', response));
}