1
00:00:09,170 --> 00:00:14,690
Hey, everyone, in this video, we're going to learn how to do Ajax with Jay Querrey and this is going

2
00:00:14,690 --> 00:00:17,620
to make our lives way, way easier.

3
00:00:18,830 --> 00:00:25,760
I wanted to show you how to do this with vanilla JavaScript or plain JavaScript like we've been doing,

4
00:00:25,970 --> 00:00:28,820
because it's important for you to know how to do it.

5
00:00:29,240 --> 00:00:34,520
But you will see that doing this using query is way easier.

6
00:00:34,550 --> 00:00:41,630
So if you are working on a website that has J query loaded, there's no reason why you shouldn't use

7
00:00:41,630 --> 00:00:41,930
J.

8
00:00:41,930 --> 00:00:44,480
Quarrie for your AJAX requests.

9
00:00:44,610 --> 00:00:49,480
So let's go back to Visual Studio Code and let's change a few things here.

10
00:00:49,640 --> 00:00:52,590
So we are going to ignore everything we've done here.

11
00:00:52,760 --> 00:00:55,160
As you can see, we have to write a lot of code.

12
00:00:55,370 --> 00:00:57,650
We need to keep track of the state.

13
00:00:57,950 --> 00:01:03,980
Then we need to test if the state is for and if the status is two hundred.

14
00:01:05,170 --> 00:01:11,070
Then we need to open the request and send it with Jake, where we won't have to do any of this.

15
00:01:11,560 --> 00:01:16,730
So the way we do this with Jake Wari, actually let me do this down here.

16
00:01:17,680 --> 00:01:22,570
So this is less than 48 Ajax with.

17
00:01:24,640 --> 00:01:25,420
Jay Querrey.

18
00:01:27,770 --> 00:01:30,530
All right, so the way we do this is the following.

19
00:01:31,860 --> 00:01:38,220
We're going to call the dollar sign here, which is what we always do with Jake Querrey, then we are

20
00:01:38,220 --> 00:01:40,610
going to call the Ajax method.

21
00:01:41,370 --> 00:01:42,300
So here it is.

22
00:01:44,100 --> 00:01:46,380
So open and close parentheses.

23
00:01:48,070 --> 00:01:54,190
And as an argument of this method, we're just going to pass an object.

24
00:01:55,230 --> 00:02:02,930
So here's the object, let's start it right now, and inside it, we need to pass some properties.

25
00:02:03,270 --> 00:02:09,710
So one of the properties is the URL property, which is going to be the URL of the request.

26
00:02:09,990 --> 00:02:11,430
So let's just copy this.

27
00:02:15,880 --> 00:02:26,140
So this is the URL, then we need to pass the type of the request which we were using the get method.

28
00:02:30,420 --> 00:02:36,300
Then something super, super interesting, let me show you this, if you pass this data type.

29
00:02:40,650 --> 00:02:47,400
As Jason, you don't even have to pass the answer because it is going to pass automatically.

30
00:02:47,790 --> 00:02:55,710
So we are telling jury that we are receiving the response in Jason and we want it as a JavaScript object.

31
00:02:56,340 --> 00:02:57,410
So that's enough.

32
00:02:58,200 --> 00:03:06,720
And now, as I've said, we don't have to keep track of the status or the state of the request because

33
00:03:06,720 --> 00:03:09,390
with Jake Wari, we have two properties.

34
00:03:09,900 --> 00:03:10,530
One.

35
00:03:11,740 --> 00:03:19,540
For if things go well and the other one, if there's any error, so the one we are going to use, if

36
00:03:19,540 --> 00:03:26,920
the status is two hundred, if we receive the response without any problem, is the success method.

37
00:03:27,960 --> 00:03:30,600
So here we are going to pass a function.

38
00:03:36,520 --> 00:03:45,220
And as an argument of this function is going to be the response text already converted into a JavaScript

39
00:03:45,220 --> 00:03:52,090
object, so we can just do this and let's do console that log just so we can check it out.

40
00:03:54,690 --> 00:04:03,120
So let's send the data to the council and then let's create another property, which will be the error

41
00:04:03,120 --> 00:04:07,290
property and same thing, we're going to have a function here.

42
00:04:09,560 --> 00:04:12,620
But this time, we don't need to pass the data.

43
00:04:14,100 --> 00:04:16,890
We just need to send to the council that.

44
00:04:18,350 --> 00:04:26,270
There's an error in the request, just so we know if things go wrong, so that's enough, we just have

45
00:04:26,270 --> 00:04:27,190
to do it like this.

46
00:04:27,200 --> 00:04:33,610
So let's compare with the amount of code that we had to write in JavaScript with Jake Wari.

47
00:04:33,770 --> 00:04:35,640
This is going to be way easier.

48
00:04:35,930 --> 00:04:41,170
We are already going to have the data in in a shava script object.

49
00:04:41,510 --> 00:04:47,030
We already have a case for success and a case for error, which is really, really cool.

50
00:04:47,510 --> 00:04:51,530
So let's comment all this art shift a.

51
00:04:53,990 --> 00:04:58,940
And let me save this, let's go back to our page to test this out.

52
00:05:00,170 --> 00:05:08,630
So as you can see, things are working well with our response and now we can type the rest of the code

53
00:05:08,630 --> 00:05:16,580
we had here, so this for the response, the answers, and then showing them separately in the console.

54
00:05:16,580 --> 00:05:24,410
But instead of just based in this inside here, I don't think we should make our request with what we're

55
00:05:24,410 --> 00:05:25,580
going to do with it.

56
00:05:27,050 --> 00:05:35,210
Because then we lose a bit of flexibility, so the best thing here would be to have a different function.

57
00:05:37,040 --> 00:05:39,680
So let's create a function called present.

58
00:05:42,990 --> 00:05:45,000
Question or something like this.

59
00:05:47,890 --> 00:05:48,750
Present.

60
00:05:48,800 --> 00:05:57,280
Missing a here so present question and right here, I'm just going to pass all that object so I can

61
00:05:57,280 --> 00:05:58,240
call it data.

62
00:06:02,620 --> 00:06:04,090
And inside here.

63
00:06:06,620 --> 00:06:10,040
I'm just going to have data here, so data.

64
00:06:17,260 --> 00:06:24,400
All right, so now we have this in a separate function, and now when we go inside the success, instead

65
00:06:24,400 --> 00:06:28,360
of sending it to the gun so we can call.

66
00:06:29,390 --> 00:06:37,280
Our function so present question, so if the request is successful, we're just going to get the response,

67
00:06:37,280 --> 00:06:43,970
which is already coming as an object, then we are going to execute this function and this function

68
00:06:43,970 --> 00:06:49,940
is going to take care of separating the question and the answers and sending it to the council.

69
00:06:49,950 --> 00:06:55,820
So if everything went right, we should be able to see the same thing we had before.

70
00:06:56,180 --> 00:06:57,080
So here it is.

71
00:06:57,290 --> 00:06:58,490
We have the question.

72
00:06:58,490 --> 00:06:59,660
We have the answers.

73
00:07:00,080 --> 00:07:01,390
This is very good.

74
00:07:01,400 --> 00:07:06,210
So we are able to move on and use Jake in our project.

75
00:07:06,440 --> 00:07:11,960
So before we finish this video, I just want to show you something real quick right here when we have

76
00:07:11,960 --> 00:07:13,530
success and error.

77
00:07:13,910 --> 00:07:21,200
This is one way of dealing with the status of the request, but there's another way of doing this.

78
00:07:21,530 --> 00:07:29,520
So instead of using properties, you could use methods that you can place right after the main method.

79
00:07:29,630 --> 00:07:32,870
So here we have the Ajax method that finishes here.

80
00:07:33,760 --> 00:07:38,000
So then after doing this, there is one called Don.

81
00:07:40,400 --> 00:07:46,580
So we can call it like this, and inside here, we are going to pass a callback function.

82
00:07:48,530 --> 00:07:50,010
We have our data.

83
00:07:54,060 --> 00:07:56,840
And here we are going to do the same thing.

84
00:07:58,320 --> 00:08:01,110
So we don't need the success.

85
00:08:02,790 --> 00:08:09,540
Property anymore, and we could do the same thing for the error, so the name of the method is actually

86
00:08:09,540 --> 00:08:10,260
fail.

87
00:08:11,450 --> 00:08:13,350
So we would do the same thing here.

88
00:08:13,640 --> 00:08:15,590
So another callback function.

89
00:08:19,760 --> 00:08:26,360
If this fails, then we can do this now, let's erase it and let's try it out.

90
00:08:26,910 --> 00:08:32,690
So now we only have three properties in this object that we are passing as an argument.

91
00:08:33,180 --> 00:08:39,290
And when this request is finished, if the status quo is two hundred, if everything went well, we

92
00:08:39,290 --> 00:08:41,440
are going to go inside this don method.

93
00:08:41,720 --> 00:08:45,100
If not, we're going to go inside this fail method.

94
00:08:45,950 --> 00:08:46,880
Let's save this.

95
00:08:49,070 --> 00:08:54,420
Let's refresh the page and as you can see, we have exactly the same result.

96
00:08:54,890 --> 00:08:57,320
I don't know which one do you prefer?

97
00:08:57,380 --> 00:09:01,750
I personally prefer to put everything inside our object.

98
00:09:01,760 --> 00:09:09,290
I think it's better, more organized like this, unless, of course, you have a great amount of code

99
00:09:09,290 --> 00:09:12,220
that should be executed on success and fail.

100
00:09:12,230 --> 00:09:17,400
And if that's the case, maybe it's better to use a separate method for our example.

101
00:09:17,630 --> 00:09:18,860
This is good like this.

102
00:09:19,010 --> 00:09:22,430
So this is how I'm going to be working with this.

103
00:09:23,160 --> 00:09:26,240
Let's save this to see if nothing broke.

104
00:09:27,570 --> 00:09:30,730
Refresh the page, I'm getting it again, so pretty good.

105
00:09:31,110 --> 00:09:32,850
We are ready to move forward.

106
00:09:32,880 --> 00:09:34,260
I'll see you in the next video.
