1var form = $('form')[0]; // You need to use standard javascript object here
2var formData = new FormData(form);
3
4or specify exact data for FormData();
5
6var formData = new FormData();
7formData.append('section', 'general');
8formData.append('action', 'previewImg');
9// Attach file
10formData.append('image', $('input[type=file]')[0].files[0]);
11
12Sending form
13
14Ajax request with jquery will looks like this:
15
16$.ajax({
17 url: 'Your url here',
18 data: formData,
19 type: 'POST',
20 contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
21 processData: false, // NEEDED, DON'T OMIT THIS
22 // ... Other options like success and etc
23});
24After this it will send ajax request like you submit regular form
25with enctype="multipart/form-data"
26
27Update: This request cannot work without type:"POST" in options since all
28files must be sent via POST request.
1<html>
2 <form id="myForm">
3 <input type="text" name="email" value="user@example.com">
4 </form>
5 <p id='text'></p>
6
7 <script>
8 window.setInterval(()=>{
9 var myForm = document.getElementById('myForm');
10 var text = document.getElementById('text');
11
12 text.innerText = myForm.elements['email'].value;
13 }, 1);
14 </script>
15</html>