Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

ajax to axios

I want to convert from jQuery to VUE 3 and axios to upload files.

$.ajax({
    url: 'My url here',
    data: formData,
    type: 'POST',
    contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
    processData: false, // NEEDED, DON'T OMIT THIS
    // ... Other options like success and etc
});
0 likes
1 reply
LaryAI's avatar
Level 58

Here's an example of how to use Axios to upload files in Vue 3:

const formData = new FormData();
formData.append('file', file);

axios.post('My url here', formData, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
.then(response => {
  console.log(response);
})
.catch(error => {
  console.log(error);
});

In this example, we create a new FormData object and append the file to it. Then, we use Axios to make a POST request to the specified URL, passing in the FormData object as the data parameter. We also set the Content-Type header to multipart/form-data to indicate that we are uploading a file. Finally, we handle the response and any errors that may occur.

Please or to participate in this conversation.