Sending a form using the FormData object - JavaScript Tutorial

  Рет қаралды 42,115

OpenJavaScript

OpenJavaScript

Күн бұрын

Пікірлер: 39
@OpenJavaScript
@OpenJavaScript 2 жыл бұрын
Thanks for watching! 👉 Source code: openjavascript.info/2022/04/26/post-form-data-using-javascripts-fetch-api/
@MegaJohn144
@MegaJohn144 Жыл бұрын
I was only looking for a way to debug my FormData object. You showed not only that but how to formulate a request and receive a response. This is a great help, as I am not that familiar with JavaScript.
@mildmildfire
@mildmildfire Жыл бұрын
Superb! Just what i was looking for!😊
@Pomyluna99
@Pomyluna99 Жыл бұрын
Thanks for that bit about how if a form field lacks the name attribute the FormData object won't include it. Couldn't solve that little mystery, much appreciated!
@OpenJavaScript
@OpenJavaScript Жыл бұрын
Yes, that's a bit of a gotcha when you start using this method. Easy to a omit a name attribute and wonder what went wrong.
@lemonade2345-j4e
@lemonade2345-j4e 3 ай бұрын
Superb, and noobs friendly!
@louleg23
@louleg23 6 ай бұрын
Really clear - thanks!
@kartikeyrana3736
@kartikeyrana3736 Жыл бұрын
thank you stranger on the net ! that was very useful :)
@dailydose8197
@dailydose8197 7 ай бұрын
Great job 🎉🎉..You explained it in a very easy way.. thank you..❤
@Khalid-sr4zo
@Khalid-sr4zo 7 ай бұрын
very nice, thank you!
@yofi2614
@yofi2614 2 жыл бұрын
Great tutorial man, wonderfull to watch, If I may one question, Where exactly in the code you insert the user file? it seems like in row number 44 in "file.files[0]", you just get the location of where to store the file in the object formData..but inserting the file itself that the user gave
@OpenJavaScript
@OpenJavaScript 2 жыл бұрын
The file is selected by the user via the input element in the HTML form. On line 43 I select this element. And on line 44 I am appending the file that exists there to the FormData object I created on 41. A user-selected file on a file input is available on the files property in array format (even when there is only one file). So for a single file, this is at position 0 in the array. Hope that helps!
@yofi2614
@yofi2614 2 жыл бұрын
@@OpenJavaScript Oh! now I got it, thanks for the quick response BTW , so, when a user upload a file, its automatically being stored in a object called "files", and what you have done in row 44 is only accessing to this specific file location by "file.files[0]", so when you append it to the formData, its like saying, create key: image and give it the data that is stored at file.files[0] (cause here youll find the file that the user uploaded,) RIGHT?
@OpenJavaScript
@OpenJavaScript 2 жыл бұрын
@@yofi2614 Exactly! It's stored on file.files in filelist format (similar to array) so you can access it by its index in that filelist. Always index position 0 for a single file :)
@tiagoborges5108
@tiagoborges5108 2 жыл бұрын
Thank you very much for me is very useful and it´s very god to know new ways to program.
@OpenJavaScript
@OpenJavaScript 2 жыл бұрын
Thank you! I'm glad you enjoyed the video :)
@modernNeanderthal800
@modernNeanderthal800 Жыл бұрын
I wasn't able to get for (item of blah) to work
@nguyenviet908
@nguyenviet908 6 ай бұрын
thank you very much
@learnershub7935
@learnershub7935 Жыл бұрын
Hello sir I was stuck in changing the object into FormData. I created the object of FormData and append the key values. but the formdata object is empty. would you like to guide me? I have also searched from many resources but still the error is not resolved.
@OpenJavaScript
@OpenJavaScript Жыл бұрын
A really common reason this can happen is that there is no "name" attribute set on input elements in the form. Only elements with this attriubte will appear in a FormData object if you create it by passing a form element into it like I do this this tutorial. You may also want to check your code against that posted for in the related blog post for this tutorial, which has a live working example: openjavascript.info/2022/04/26/post-form-data-using-javascripts-fetch-api/
@DubPlayer1
@DubPlayer1 8 ай бұрын
THANK YOU
@m3hdim3hdi
@m3hdim3hdi 2 жыл бұрын
is formdata still used or there is something better?
@OpenJavaScript
@OpenJavaScript 2 жыл бұрын
The FormData constructor is still used. It's a native tool for the job and a really good way to parse form data. Your other alternative is, if your data is okay in pure string format, send to the server as URL parameters (kzbin.info/www/bejne/bXPFfWWGj5pmb5Y).
@m3hdim3hdi
@m3hdim3hdi 2 жыл бұрын
@@OpenJavaScript thank you so much for replay
@sg-ck2oh
@sg-ck2oh 2 жыл бұрын
How was the server made or was it nodejs?
@OpenJavaScript
@OpenJavaScript 2 жыл бұрын
It isn't a server I created. It's a live test server for creating HTTP requests that mirrors the information you send it in the response (so you can see what the server sees). Here's the homepage: httpbin.org/
@عوالممدهشة-ت7و
@عوالممدهشة-ت7و 2 жыл бұрын
When i send the form with images it gives me this error unsupported media
@OpenJavaScript
@OpenJavaScript 2 жыл бұрын
In that case, you may want to try embedding the file in a Blob object before appending it to the FormData object then: const blob = new Blob([yourFile], { type: mimeType }); A Blob (binary large object) is a universally recognized way to store and transfer files of any type, so hopefully this solves your problem...
@عوالممدهشة-ت7و
@عوالممدهشة-ت7و 2 жыл бұрын
@@OpenJavaScript when i send it it give me missing content-type boundary
@spoiler3537
@spoiler3537 Жыл бұрын
Great explanation . Can show the server side code as well ?
@OpenJavaScript
@OpenJavaScript Жыл бұрын
I'll keep it in mind to show more server-side code in future, but this server in particular is a live public test server that I do not control. But it is often useful because it mirrors what you send it back and viewers can make the same requests I am without any CORS error. However, if you are interested in learn more about handling uploads server-side, you might find my more recent tutorial on handling file uploads in Node.js useful: kzbin.info/www/bejne/iovZfn-Ne7uJnqc
@patshalaaa
@patshalaaa Жыл бұрын
can we send multiple images ?
@patshalaaa
@patshalaaa Жыл бұрын
use multer please to demonstrate how we can crop each image and send to to server using multer
@modernNeanderthal800
@modernNeanderthal800 Жыл бұрын
Set a variable to [...formData]. In the latest version of node, node 18.x.x. is on my machine and formData wasn't iterable. I had to set it to a variable `let formValues = [...formData];` wish I understood it better. Okay on to the next thing. Gg yall, comment.
@keithramatlhape8789
@keithramatlhape8789 Ай бұрын
It was also giving me an issue. I used: ` for (let [name, value] of formData) { console.log(name, value) } `
@modernNeanderthal800
@modernNeanderthal800 Ай бұрын
@keithramatlhape8789 thanks!!!
@newworld4252
@newworld4252 Жыл бұрын
Lol this is very advanced.. let me keep scrolling to my learning level 😢
@nevestunga955
@nevestunga955 Жыл бұрын
GOOD!!!!!1
@OpenJavaScript
@OpenJavaScript Жыл бұрын
Thanks!
Using FormData Objects Effectively
13:14
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 37 М.
Read a file using the FileReader API - JavaScript Tutorial
25:48
OpenJavaScript
Рет қаралды 23 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
How do you Submit an HTML Form?  How does it work?
17:02
Web Dev Cody
Рет қаралды 88 М.
POST Form Data as JSON with Fetch API in JavaScript
9:25
ByteGrad
Рет қаралды 68 М.
I tried 8 different Postgres ORMs
9:46
Beyond Fireship
Рет қаралды 440 М.
The Correct Way to Use Form Data in React
17:29
Cosden Solutions
Рет қаралды 15 М.
FormData API - React
17:26
Coding Addict
Рет қаралды 18 М.
Save user form input data in a JavaScript object
7:10
OpenJavaScript
Рет қаралды 45 М.
10 Form Validation Tips Every Web Developer SHOULD KNOW!
9:22
James Q Quick
Рет қаралды 18 М.
Blobs and Blob URLs | JavaScript Tutorial
15:32
OpenJavaScript
Рет қаралды 30 М.
JSON and AJAX Tutorial: With Real Examples
40:45
LearnWebCode
Рет қаралды 1,8 МЛН
Learn JavaScript With This ONE Project!
1:10:26
Tech With Tim
Рет қаралды 771 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН