Now, that's it for this course section
about Ajax requests.
Again, a lot of new pieces of knowledge.
In the end,
Ajax requests are just behind the scenes requests
which you send directly
from inside your browser side JavaScript code.
And they call them behind the scenes because they happen
without the user necessarily noticing them.
The page doesn't reload.
Instead, you send your own request
and you handle the response manually
in your browser side code then
just as we did it in this course section.
Now, you did learn that there is the fetch function,
which we used in this course section,
but I also mentioned the XML HTTP request object.
And even though we didn't use it here,
and even though the object itself
is a bit more clunky to use,
I absolutely wanna highlight
that Axios is a very popular library
based on XML HTTP request which is being used a lot
and which is a great alternative to the fetch function.
So you absolutely might wanna check out
this library as well,
explore the official documentation to learn how it works,
to see how you could use that instead of the fetch function
for sending requests, handling responses,
handling errors and so on.
It's also really, really good.
Nonetheless here, we focused on fetch
and we learned how we can send get requests with it,
but how we can then also send post requests.
There, it's important to include a body in JSON format
and that we should set the appropriate header
to make sure that on the server side,
we're parsing that incoming data correctly.
And speaking of JSON, that is simply just a data format.
It's, in the end, just text,
but text formatted in a certain way
which is both human and machine readable.
It looks like this
and therefore very much like JavaScript objects,
but it isn't a JavaScript object.
It's just text looking like one with slight differences,
like the double quotes around the key names
and the double quotes around strings.
Single quotes aren't allowed here.
Now, we did all the talk about handling responses,
which we get back because that's a key thing
when working with Ajax requests.
You typically wanna do something
once the request succeeded.
We learned how we can check whether it succeeded
and how we can then run different pieces of code
based on the request result
and based on the response being successful or not.
We learned that we can update the DOM
and for example output our comments there
if we got comments successfully,
and we also learned that we can differentiate
between response errors
where something went wrong on the server
but we got a response in the end,
and technical errors where for example the request
doesn't even leave our machine.
And that we wanna handle both,
the latter category of errors can be handled with try-catch
when using async await.
And that's also important we use async await
because the fetch function gives us a promise.
It does not give us a response instantly
because sending a request is not an instant option.
It's an asynchronous task instead.
And promises, as we learned before in the course,
are a common thing, a common solution in JavaScript
for dealing with asynchronous code.
The fetch function embraces this solution.
It embraces promises.
And therefore, we can also use async await
to handle these promises in a more convenient way.
Last but not least, we learned about these new HTTP methods,
which we actually didn't use in this course section,
but which you will see later in the course.
And therefore, you should already be aware of them
as mentioned in the last lecture.
And with all of that,
we now have another very powerful and very important tool
in our toolbox because Ajax requests,
so these JavaScript-driven HTTP requests,
are also something which you will see a lot
in web development in this course,
and therefore something you have to know
as a web developer.