1 00:00:00,000 --> 00:00:04,990 All right, in the last lesson we looked at a jQuery GET request. 2 00:00:04,994 --> 00:00:08,644 And in this lesson we're going to look at a jQuery post request. 3 00:00:08,640 --> 00:00:10,770 And this is how we send data to a server and ask 4 00:00:10,770 --> 00:00:13,320 it to update data or create some new data. 5 00:00:13,320 --> 00:00:15,210 And this is really, really simple to, 6 00:00:15,210 --> 00:00:19,220 it's just dot post and then some URL in here, 7 00:00:19,215 --> 00:00:20,955 some data in here, 8 00:00:20,955 --> 00:00:25,665 and a callback function with the data that's going to be returned. 9 00:00:26,080 --> 00:00:28,310 That's literally all it is. 10 00:00:28,310 --> 00:00:31,130 So your data should be an object of some kind. 11 00:00:31,130 --> 00:00:35,870 So we get say, Hello world. 12 00:00:35,870 --> 00:00:39,970 And that URL we need somewhere to post to. 13 00:00:39,965 --> 00:00:43,525 And it's really, really hard to find a place to safely posting, 14 00:00:43,520 --> 00:00:46,660 but I found one called HTTP bin. 15 00:00:46,655 --> 00:00:49,045 So we do this as a URL, 16 00:00:49,040 --> 00:00:56,920 HTTPS colon slash slash HTTP bin.org slash post. 17 00:00:56,915 --> 00:00:59,185 And if we want to, we can put these on 18 00:00:59,180 --> 00:01:03,300 separate lines just to make this look a little nicer. 19 00:01:07,300 --> 00:01:15,290 And then we can say console.log post was complete with this data. 20 00:01:15,290 --> 00:01:23,900 Console.log data post was complete with this data. And let's take a look. 21 00:01:23,900 --> 00:01:25,960 This is all the data that we got back. 22 00:01:25,955 --> 00:01:34,295 An HTTP, HTTP bin.org tends to give us all of our data back in the form of property here. 23 00:01:34,295 --> 00:01:35,605 So it says hello world, 24 00:01:35,600 --> 00:01:37,300 we can put anything in there though. 25 00:01:37,295 --> 00:01:45,475 We get say, Instagram is equal to at coding dot for dot everybody. 26 00:01:45,470 --> 00:01:50,400 Which if you want free coding tips and tricks definitely come check this out. 27 00:01:51,690 --> 00:01:56,350 And form Instagram coding for everybody. 28 00:01:56,350 --> 00:01:58,360 And so we know that that is actually working. 29 00:01:58,360 --> 00:02:00,760 Now, if we want to make sure that it's not working, 30 00:02:00,760 --> 00:02:02,490 we could always just try get. 31 00:02:02,485 --> 00:02:04,735 We'll do dot get and will do it, 32 00:02:04,735 --> 00:02:10,845 will create a get request to the slash post URL. 33 00:02:10,840 --> 00:02:13,230 And you can see four or five, 34 00:02:13,225 --> 00:02:14,685 that means not allowed. 35 00:02:14,680 --> 00:02:16,000 You're not allowed to do that. 36 00:02:16,000 --> 00:02:18,360 It has to be a post request. 37 00:02:18,355 --> 00:02:19,875 So go ahead and give this a shot. 38 00:02:19,870 --> 00:02:21,580 Use HTTP bin.org. 39 00:02:21,580 --> 00:02:23,440 Don't forget you need HTTPS. 40 00:02:23,440 --> 00:02:25,390 You might run into that error if it's coming from 41 00:02:25,390 --> 00:02:27,760 an unsecure location to a secure location, 42 00:02:27,760 --> 00:02:29,500 just make sure you have that S and there. 43 00:02:29,500 --> 00:02:31,200 This is called your payload. 44 00:02:31,195 --> 00:02:34,065 This is the data that you're going to be sending them. 45 00:02:34,060 --> 00:02:36,660 And then you've got a callback function when it's done, 46 00:02:36,655 --> 00:02:38,445 what are you doing with that data? 47 00:02:38,440 --> 00:02:41,880 All we're doing here is we're doing console.log data. 48 00:02:41,875 --> 00:02:47,205 Now this is really, really useful when you're creating a single page application or 49 00:02:47,200 --> 00:02:50,070 progressive web app where you need someone to 50 00:02:50,065 --> 00:02:53,625 register or log in and you don't want to take them to another page, 51 00:02:53,620 --> 00:02:55,330 or you have an API endpoint, 52 00:02:55,330 --> 00:02:58,330 or they have to log in through a different website, something like that. 53 00:02:58,330 --> 00:03:01,480 You can send that data somewhere else for processing. 54 00:03:01,479 --> 00:03:04,119 And that data can come back usually in the form of JSON. 55 00:03:04,120 --> 00:03:07,800 And then JQuery can handle it very gracefully. 56 00:03:07,795 --> 00:03:09,745 In the next lesson, let's go ahead and create 57 00:03:09,745 --> 00:03:13,005 a random Star Wars character generator based 58 00:03:13,000 --> 00:03:17,330 on pretty much everything we have done in this course.