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

shahr's avatar
Level 10

ajax

I want to convert from jQuery to JavaScript 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

To upload files using JavaScript instead of jQuery, you can use the XMLHttpRequest object. Here's an example:

const xhr = new XMLHttpRequest();
const formData = new FormData();

formData.append('file', fileInput.files[0]);

xhr.open('POST', 'My url here');
xhr.send(formData);

In this example, fileInput is a reference to the file input element on the page. You can add additional data to the formData object using the append method.

Note that the contentType and processData options are not needed when using FormData.

Please or to participate in this conversation.