Okey lets move on, Before sending the HTTP POST request, by using the Fetch API we need to prepare the new object based on what has been entered in the form. Quick thought, On the form don't forget to call the event.preventDefault()method to prevent the default submit behaviour of the browser. as you can see to fetch our database we did the same thing to fetch the items which in this case are Sushi rolls.īut what about if we want to now create items and add to out backend ? well first we need to create our form then grab the values of this form to send to the backend. what I did is iterate over and chose the first Shop and this first shop has Items. from there we can choose what todo with it. see code belowĪs you see, the data was fetched and we now have the data we wanted. then method we then took the response and converted it to JSON or JavaScript Object Notation is the format for how we transmit data through web applications. If we enter the above code in the console will return a promise, every successful Fetch request will always return a promise and along with this promise comes a response using the. This is how a basic fetch function looks like. In this blog we are going to go over two of this fetches a GET and POST using my JS project. There are four types of requests that Fetch provides us with:ġ)GET requests - used for retrieving or “fetching” data.Ģ)POST requests - used for uploading or adding data.ģ)PATCH / PUT requests - used for editing data that is already in the database.Ĥ)DELETE requests - used for deleting a piece of data. i was like "what does Mean girls, have to todo with coding!"īut its actually a word that has a different meaning of what the movie implies, let check this fetch out and what it does in JavaScript.įetch() in its simplest definition, is a function in JavaScript for interacting with the database, especially with APIs (Application Programming Interface). Yes, the first thing that came to my mind as i heard the word fetch in JavaScrip.
0 Comments
Leave a Reply. |