1
00:00:00,170 --> 00:00:02,240
So you can see side by side.

2
00:00:02,240 --> 00:00:08,510
I opened my Visual Studio Code editor and I opened my browser using Live server extension, and I already

3
00:00:08,510 --> 00:00:16,760
created an HTML file in my directory, and I want to load a text file in my index.html file using h6.

4
00:00:16,970 --> 00:00:24,920
So I create a text file name text dot txt inside content directory and you can see inside this text

5
00:00:24,920 --> 00:00:26,960
file I write some text.

6
00:00:26,990 --> 00:00:28,730
Hello, I am text file.

7
00:00:29,090 --> 00:00:33,980
So I'm going to load this file in my index.html file using h6.

8
00:00:34,430 --> 00:00:38,570
First I'm going to create a paragraph tag and our id is demo.

9
00:00:39,510 --> 00:00:42,270
And insert this paragraph I'm going to type here.

10
00:00:42,270 --> 00:00:43,170
We load data.

11
00:00:43,320 --> 00:00:45,210
And now I'm going to create a button.

12
00:00:45,210 --> 00:00:53,190
So whenever we click the button it brings all the text from Text.txt file and print inside the paragraph

13
00:00:53,220 --> 00:00:53,820
tag.

14
00:00:54,180 --> 00:00:56,160
And our button name is click.

15
00:00:56,160 --> 00:01:00,210
And I'm going to use onClick event inside onClick.

16
00:01:00,210 --> 00:01:03,060
I'm going to use a function load data.

17
00:01:03,060 --> 00:01:06,750
Inside this function we are going to create our A6.

18
00:01:06,750 --> 00:01:10,890
And if I save this file you can see in my browser it prints our static data.

19
00:01:10,890 --> 00:01:13,740
So at first I'm going to create script tag.

20
00:01:13,740 --> 00:01:18,180
And inside the script tag I'm going to create a function named Load data.

21
00:01:18,180 --> 00:01:25,470
So as I told you earlier first we need to create an object using XML http request class.

22
00:01:25,470 --> 00:01:31,530
So I'm going to take a variable which is x http equal to new.

23
00:01:32,570 --> 00:01:34,760
XML Http request class.

24
00:01:35,390 --> 00:01:37,670
You can take any name for your variable.

25
00:01:38,300 --> 00:01:40,700
Then I'm going to open my object.

26
00:01:41,370 --> 00:01:47,130
So I'm going to use x http dot open function as a parameter.

27
00:01:47,130 --> 00:01:49,170
First we need to declare our method.

28
00:01:49,170 --> 00:01:51,510
And I'm going to use get method.

29
00:01:51,750 --> 00:01:55,530
And then we need to declare which file I want to access.

30
00:01:55,530 --> 00:01:57,090
I mean our file path.

31
00:01:57,330 --> 00:02:02,460
First I'm going to type our folder name then slash text dot txt.

32
00:02:02,880 --> 00:02:05,280
And then we need to send boolean parameter.

33
00:02:05,280 --> 00:02:09,660
If you want to use synchronized mode then you need to send true value.

34
00:02:09,690 --> 00:02:11,430
So I'm going to pass true.

35
00:02:11,790 --> 00:02:19,470
To send this request I'm going to use another method dot send x http dot send.

36
00:02:19,680 --> 00:02:23,160
From here our request will send to the server.

37
00:02:23,160 --> 00:02:27,330
If we send request to server a response will come from there.

38
00:02:27,330 --> 00:02:31,710
So to check this response I'm going to write some code.

39
00:02:31,830 --> 00:02:36,420
At the same time we will also check our status stage.

40
00:02:36,750 --> 00:02:40,950
For this I'm going to take my object name dot.

41
00:02:40,950 --> 00:02:44,850
I'm going to use a method on ready state change.

42
00:02:45,420 --> 00:02:48,510
And then I create a function something like that.

43
00:02:48,660 --> 00:02:54,690
The function will continuously run whenever on ready state change its value.

44
00:02:54,690 --> 00:03:00,510
And inside this function we need to check response has came or not when our registered value would be

45
00:03:00,510 --> 00:03:03,390
four, it mean we get our response.

46
00:03:03,570 --> 00:03:06,840
So I'm going to run if condition inside this function.

47
00:03:07,140 --> 00:03:18,210
If inside the round braces this dot readystate equal to equal to four, and this dot status equal to

48
00:03:18,210 --> 00:03:19,410
equal to 200.

49
00:03:19,440 --> 00:03:21,330
This means our object.

50
00:03:21,660 --> 00:03:23,370
We already learned about it.

51
00:03:23,550 --> 00:03:25,020
Just remember one thing.

52
00:03:25,020 --> 00:03:28,440
We check server response with ready state.

53
00:03:28,830 --> 00:03:34,740
And the exact file which we want to access is available in the server or not.

54
00:03:34,740 --> 00:03:43,140
For check the status we use dot status and if the status is 200, it mean the file is available in our

55
00:03:43,140 --> 00:03:45,540
server and inside the function.

56
00:03:45,540 --> 00:03:53,070
I want to show you the exact response in my console console dot log inside the round braces this dot

57
00:03:53,070 --> 00:03:54,300
response text.

58
00:03:54,840 --> 00:04:00,570
Using these dot response text property, we can print the server response in our console.

59
00:04:00,840 --> 00:04:02,880
Let me explain it once again.

60
00:04:02,880 --> 00:04:09,330
First, I create a button in my HTML page and assign a function on this button name load data.

61
00:04:09,330 --> 00:04:13,920
And then I create this function inside my script tag and inside this function.

62
00:04:13,920 --> 00:04:16,860
First I create an object name Xrdp.

63
00:04:17,160 --> 00:04:24,210
Using this object, we can send request to our server and I want to send request for text dot txt file

64
00:04:24,420 --> 00:04:32,370
and using send method we send our request and then I use on ready state change property to check server

65
00:04:32,370 --> 00:04:38,490
status and when server status match with four then insert this console it, print the text.

66
00:04:38,640 --> 00:04:43,200
So let's save the file and I'm going to click the button.

67
00:04:43,200 --> 00:04:48,600
And you can see in my console it print all the text from our text dot txt file.

68
00:04:48,600 --> 00:04:51,510
And if I change anything in our text file.

69
00:04:52,140 --> 00:04:55,530
So I type Hello World and save this file.

70
00:04:55,530 --> 00:05:01,530
You can see in my browser it automatically reload our browser because we are using live server and this

71
00:05:01,530 --> 00:05:03,570
file is connected with our server.

72
00:05:03,570 --> 00:05:08,100
And if I click this button you can see the updated content.

73
00:05:08,580 --> 00:05:12,210
So you can see which we had requested from the server.

74
00:05:12,210 --> 00:05:13,710
It is working properly.

75
00:05:13,710 --> 00:05:20,850
And if you want to show this message in your web page, not in console, then you need to use document

76
00:05:20,850 --> 00:05:21,540
method.

77
00:05:22,080 --> 00:05:28,980
I'm going to comment out this console dot log line and type document dot get element by ID.

78
00:05:29,250 --> 00:05:31,050
Insert the round braces.

79
00:05:31,050 --> 00:05:37,170
I'm going to provide our paragraph ID demo dot inner HTML.

80
00:05:37,980 --> 00:05:41,250
Equal to this dot response text.

81
00:05:41,730 --> 00:05:43,260
And if I save this file.

82
00:05:44,580 --> 00:05:46,080
And click on this button.

83
00:05:46,110 --> 00:05:48,900
You can see it print the text in our browser.

84
00:05:49,290 --> 00:05:51,110
Using inner HTML method.

85
00:05:51,120 --> 00:05:54,990
We can change HTML content inside HTML tag.

86
00:05:54,990 --> 00:05:59,190
But the question is if we receive error status, then what?

87
00:05:59,640 --> 00:06:02,850
For this we need to create another elseif condition.

88
00:06:03,240 --> 00:06:07,650
So I'm going to duplicate this if condition and change if with elseif.

89
00:06:07,980 --> 00:06:14,970
If our status is 404 and I don't want to show 404 message to my client.

90
00:06:14,970 --> 00:06:19,200
So insert the inner HTML I'm going to set not found.

91
00:06:19,530 --> 00:06:24,900
If the text file not available in our server then it is going to print not found.

92
00:06:25,080 --> 00:06:26,700
And I'm going to save this file.

93
00:06:27,800 --> 00:06:28,910
And click the button.

94
00:06:29,610 --> 00:06:35,340
You can see it written our file content because this file is available in our server.

95
00:06:35,990 --> 00:06:43,310
But if I change our file name and I'm going to change my file name text one dot txt and save the file

96
00:06:43,940 --> 00:06:46,400
and back to my index.html page.

97
00:06:47,300 --> 00:06:54,350
And if I click on this button, you can see in my browser it print not found, but inside my console

98
00:06:54,350 --> 00:06:56,490
it print 404.

99
00:06:56,510 --> 00:07:01,070
So you can set condition for different type of Http response.

100
00:07:01,370 --> 00:07:08,180
And if I change the file name text one dot txt and try to print this content in our browser, you can

101
00:07:08,180 --> 00:07:11,090
see this time it print our content.

102
00:07:11,570 --> 00:07:15,420
And now I want to show one thing with our console tag.

103
00:07:15,440 --> 00:07:18,140
There is another tag named network.

104
00:07:18,960 --> 00:07:24,870
When we work with Asics, it sends response in our x air tag.

105
00:07:24,900 --> 00:07:25,710
X mean.

106
00:07:25,950 --> 00:07:26,580
XML.

107
00:07:26,610 --> 00:07:27,270
H mean.

108
00:07:27,300 --> 00:07:30,030
Http r means response.

109
00:07:30,990 --> 00:07:38,100
You can see in response we get a file from a server named text one dot txt and its server status is

110
00:07:38,100 --> 00:07:38,970
200.

111
00:07:39,000 --> 00:07:42,870
It's a Ajax request, so the type is XHR.

112
00:07:42,870 --> 00:07:46,260
And if I click on the text file you can see the preview.

113
00:07:46,260 --> 00:07:48,420
And also you can see the response.

114
00:07:48,420 --> 00:07:54,540
And also you can see the timing here you can see how long it took to come from the server.

115
00:07:54,660 --> 00:08:02,040
And if you click on the header you can see the URL path request method get status code okay.

116
00:08:02,040 --> 00:08:04,380
And also you can see my remote address.

117
00:08:05,430 --> 00:08:11,670
This was normal text, but in actual life we use Json data for practice purpose.

118
00:08:11,670 --> 00:08:13,860
We can retrieve fake Json data.

119
00:08:14,710 --> 00:08:22,660
There are lots of websites who can provide fake Json data, and I'm going to use this website Json placeholder.

120
00:08:22,840 --> 00:08:27,040
And if you scroll down a little bit you can see resource option.

121
00:08:27,040 --> 00:08:29,270
And I'm going to use post data.

122
00:08:29,290 --> 00:08:33,130
It's provide us 100 demo data something like that.

123
00:08:33,160 --> 00:08:34,690
This also are dummy data.

124
00:08:34,690 --> 00:08:37,900
And now I want to show all of this post in our website.

125
00:08:37,930 --> 00:08:44,080
For this I'm going to copy all the URL and I'm going to pass this URL in our open method.

126
00:08:45,460 --> 00:08:51,730
And I'm going to save this file and back to my index file, and I'm going to open my console once again.

127
00:08:52,550 --> 00:08:57,470
Let's open the network tab and I'm going to show my HR status.

128
00:08:58,100 --> 00:09:03,890
And now I'm going to click the button and you can see it print out all post in my browser.

129
00:09:04,340 --> 00:09:07,580
And you can see it print out all the post in my browser.

130
00:09:07,970 --> 00:09:19,490
And also you can see file name, post status, 200 type x hr, file size 7.7 KB and response time six

131
00:09:19,490 --> 00:09:20,770
24 millisecond.

132
00:09:20,780 --> 00:09:23,180
And you can see lots of data in our browser.

133
00:09:23,480 --> 00:09:27,830
We cannot understand it properly and I'm going to print it in our console.

134
00:09:28,900 --> 00:09:31,930
And I'm going to just uncomment this console.log line.

135
00:09:32,380 --> 00:09:35,890
And I don't want to print all the data in our browser.

136
00:09:36,130 --> 00:09:39,720
I just want to print in our console and I'm going to save this file.

137
00:09:39,730 --> 00:09:41,420
Let's hit the click button.

138
00:09:41,440 --> 00:09:47,110
Now you can see in our console it print all the data very well formatted.

139
00:09:47,470 --> 00:09:51,490
You can see different titles different ID etcetera.

140
00:09:51,730 --> 00:09:57,480
So using Asics without refreshing our page we can retrieve data from our server.

141
00:09:57,490 --> 00:10:01,640
And last thing I want to say you can see it's a long method.

142
00:10:01,660 --> 00:10:03,580
It can complicate our code.

143
00:10:03,970 --> 00:10:07,960
So most of the developers using jQuery to run Asics.

144
00:10:08,290 --> 00:10:15,580
Otherwise developers use a new method named fetch which is introduced in JavaScript ES6 version.

145
00:10:16,000 --> 00:10:19,210
So in the next video we are going to learn about fetch API.

146
00:10:19,570 --> 00:10:21,040
Thanks for watching this video.

147
00:10:21,040 --> 00:10:23,170
Stay tuned for our next tutorial.
