jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
Skills:
JavaScript Fundamentals80%
Key Takeaways
Sends data to a backend server using jQuery Ajax post method and a RESTful API
Full Transcript
all right we're on video number two of our jQuery ajax series uh in the last uh video what we did is we covered how to do a get request to grab all of our orders from the server and spit them out so what we have here is an empty UL of orders no liis in it we're on page load we are grabbing all the orders and we're spitting out An Li for each one so that's what's going on here and you see if I inspect this element let's make this a little bigger here you see that I got two liis in here we have populated An Li for Will and An Li for Laura yippe so that's done let's go on and move into the next part which is posting our new drinks say I want to do a drink for John and he just gets a coffee I want to be able to add that I want that to do something I want it to add it to the backend server so it's now per permanently in the database and I also wanted to add it on the page when that's successful let's go ahead let me show you one more thing here on this get request or the jQuery ajax should I say is you can also do error and I can do an erir if this thing does not work I can alert error loading ORD so I have success and I have error if I refresh nothing happens because it was successful uh but let's say I give this a bad URL here two s's on accident alert error loading orders and nothing happened cool so I'll leave that in there and let's go into the next part we want to post new things to the backend so let's look at here from the backend team they said to add an order you send a post request to the same URL API orders except for we're not doing a get request we're doing a post request and which means we're sending information up and here's the sample post data that we are sending we're sending a Json post um and we're sending this information and it will respond kind of like a get request it will also have a response will have the item with an ID now which is if it's successful so we can use that ID to reference it later if we want to update our beverage order so let's go ahead and put that in so we have a button here the ID is ADD order so on click of add order we want to do something we want to make a post let's go ahead and grab our information up top let me cach this Dom every time we add an order we're going to want to look at these two fields and get the value out so I'm going to cash these two Fields up top and I think they're called name Yep they're called name and drink there you go name equals name and drink so on ADD order I'm going to get those two values out of here and then I'm going to post that order so let's go ahead and put together our little posting let's see from the backend team we want to post a simple object of name and drink so let's build that object and name. vow easy enough and then drink drink. valow so that's our order right there we have our name and our drink we're getting the value out of these two fields and we're moving on and then we're going to do Ajax again this type is going to be a post not a get which means we're sending information URL is the same and then we also have a data field since we are passing data in we're going to give it our order right there our order object and a success function and so the order you can actually pass it an existing object or you can actually just kind of create an object on the fly like this if we were to do that that would be the exact same thing we're creating a brand new object it's a little cleaner in my opinion to build the object up there and then so this stays clean um so on success this is our new order that they have given us back um we can do the exact same thing here orders. append New Order and New Order so we are doing is we're posting our new order to the back end we're going to wait for that to be successful if it's successful we are going to add a new Li to our list so it shows up on our page and I'll also add an error and we'll just call this alert error saving order not message there we go let's see if this works so uh oh I have a JavaScript error here let's find out what that's all about unexpected token online 24 yep I did bad syntax those of you guys who are watching the video watching that I cannot type and talk at the same time are probably laughing out loud at me okay so our two drinks are populated in let's add a new one John just likes coffee haha it did it let's go and look at our Network Tab and see what just happened so you notice we did our initial get of orders and then we did a little while later we did a post to the same API and let's look at that real quick you'll notice we posted this information of name John drink coffee and then we got a response of name John drink coffee and an ID of three and so now watch what happens when we refresh our page we've saved it to the back end and now we're getting three drinks so if I had two more drinks Bobby likes water not sure why there we go so now if I refresh now we have all four drinks awesome we have a fully functioning app that talks to the back end and so one thing I do not like that's going on here is I don't really like that we are copying this EXA basically this exact same thing over again so let's go ahead and pull that out we're going to make a function called add order and this will just take an order so let's get rid of this right here cut you out paste you in here order. name so basically whatever we pass it is going to be calling it order so now I just have to go add order and give it my order same thing down here hate that we're doing the exact same thing except for we're going to be passing it the New Order In this case that should work just fine so I refresh it's still populating my initial orders just fine and if I do a new one drink will be latte there we go that's working just fine as well so now what we've done is we have pulled this out to uh a single function here um but if we want to get much more complicated than this it's going to get really messy I really want to start adding more elements in here like a paragraph gra tag and some strong tags but that's just going to be a giant messy string we really want to do this with a templating engine so let's go ahead and add in Hogan JS into our next ah we'll use mustache JS let's go Ahad and add mustache JS into our next video and then we'll add in the delete functionality to wrap up our application
Original Description
Free RESTful API to use for practice!: http://rest.learncode.academy/
In this jQuery AJAX Tutorial for beginners, we're going to be adding new data to our backend server with the jQuery ajax post method. jQuery ajax post allows us to send data for the backend to save. When the backend is setup in a RESTful way, it will return our successful post along with an ID and any other information it created for us when it saved the information. We'll use this information to display our new post to our user.
Lesson #1: jQuery Tutorial for Beginners
https://www.youtube.com/watch?v=hMxGhHNOkCU
Lesson #2: Listen to user events and respond with jQuery actions!
https://www.youtube.com/watch?v=G-POtu9J-m4
Lesson #3: Clean up the jQuery by putting some data in the HTML
https://www.youtube.com/watch?v=Cc3K2jDdKTo
Lesson #4: "DOM Traversal" with jQuery
https://www.youtube.com/watch?v=LYKRkHSLE2E
Lesson #5: Building a jQuery Tab Panel Widget
https://www.youtube.com/watch?v=1nWrIBB_bMA
Lesson #6: Building a jQuery Slider / DOM Caching
https://www.youtube.com/watch?v=KkzVFB3Ba_o
Lesson #7: JQuery Ajax tutorial #1
https://www.youtube.com/watch?v=fEYx8dQr_cQ
-~-~~-~~~-~~-~-
Also watch: "Responsive Design Tutorial - Tips for making web sites look great on any device"
https://www.youtube.com/watch?v=fgOO9YUFlGI
-~-~~-~~~-~~-~-
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from LearnCode.academy · LearnCode.academy · 29 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
▶
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
LearnCode.academy
Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
LearnCode.academy
How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
LearnCode.academy
Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
LearnCode.academy
Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
LearnCode.academy
CSS Tutorial - Web Development Tutorial for Beginners (#5)
LearnCode.academy
Node.js tutorial for beginners - an introduction to Node.js with Express.js
LearnCode.academy
Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
LearnCode.academy
Javascript Tutorial - Programming Tutorial for Beginners Pt 1
LearnCode.academy
Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
LearnCode.academy
AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
LearnCode.academy
WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
LearnCode.academy
YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
LearnCode.academy
BOWER! - Streamline Web Workflow with Bower Package Manager
LearnCode.academy
Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
LearnCode.academy
GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
LearnCode.academy
GITHUB PULL REQUEST, Branching, Merging & Team Workflow
LearnCode.academy
Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
LearnCode.academy
jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
LearnCode.academy
jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
LearnCode.academy
jQuery Tutorial #1 - jQuery Tutorial for Beginners
LearnCode.academy
Node.js MongoDB Tutorial using Mongoose
LearnCode.academy
Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
LearnCode.academy
WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
LearnCode.academy
jQuery Tutorial #4 - DOM Traversal with jQuery
LearnCode.academy
jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
LearnCode.academy
jQuery Tutorial #6 - Building a jQuery Image Slider
LearnCode.academy
jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
LearnCode.academy
jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
LearnCode.academy
jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
How to put your website online - how to FTP to a domain & upload files to a webhost
LearnCode.academy
Basic Terminal Usage - Cheat Sheet to make the command line EASY
LearnCode.academy
SSH Tutorial - Basic server administration with SSH
LearnCode.academy
Vagrant Tutorial - Running a VM For Your Local Development Environment
LearnCode.academy
Sublime Text Favorite Packages and Workflow
LearnCode.academy
What Makes Javascript Weird...and AWESOME - Pt 1
LearnCode.academy
Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
LearnCode.academy
Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
LearnCode.academy
FREE REST API - Practice Developing Javascript AJAX Apps with this API
LearnCode.academy
Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
LearnCode.academy
Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
LearnCode.academy
Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
LearnCode.academy
Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
LearnCode.academy
Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
LearnCode.academy
Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
LearnCode.academy
Web Development Advice - Interview with Dev Tips
LearnCode.academy
How the Internet Works for Developers - Pt 2 - Servers & Scaling
LearnCode.academy
How the Internet Works for Developers - Pt 1 - Overview & Frontend
LearnCode.academy
HAPROXY vs NGINX - 10,000 requests while killing servers
LearnCode.academy
Node.js Cluster - Boost Node App Performance & Stability with Clustering
LearnCode.academy
Web Dev Training with Treehouse
LearnCode.academy
What is Node.js Exactly? - a beginners introduction to Nodejs
LearnCode.academy
How to deploy node.js applications #1 - spin up a server
LearnCode.academy
Deploying node.js applications #2 - provision server & setup flightplan
LearnCode.academy
Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
LearnCode.academy
Mobile Web Design - Coding Workflow For Mobile Websites
LearnCode.academy
WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
LearnCode.academy
GRUNT TUTORIAL - Grunt makes your web development better!
LearnCode.academy
STOP USING FTP! - How to Deploy with Flightplan over SSH
LearnCode.academy
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Dev.to · Ayush Shekhar
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Dev.to · Alejandro
HTML Canvas Cheat Sheet for Pixel Art & Image Effects
Dev.to · lemon
Draw Pixel Art with Vanilla JS — A Step-by-Step Guide
Dev.to · lemon
🎓
Tutor Explanation
DeepCamp AI