Now to conclude this course section
there's one last pretty important feature
of Ajax requests I want to dive in.
And that is the fact that when working with Ajax,
so with those JavaScript-driven HTTP requests,
you actually have more HTTP methods available.
More HTTP methods you can use for those requests.
But let's take a step back.
We know about Get and Post requests.
We use them all the time throughout this course.
We know that a Get request is used to fetch data,
and we typically send a Post request
if we wanna express our intention to store some data.
I also will emphasize again,
as I did before in the course,
that the request method alone doesn't do anything.
It's always up to your server side code
to decide what happens exactly.
You write your own server side code.
So it's that service side code
that decides what should happen
if it receives an incoming Post request.
You could send a Get request to delete posts, for example.
The method alone doesn't imply anything.
You just typically follow certain conventions
to use Post requests if you change something on the server,
or if you store data, and Get requests
if you want to fetch data.
But it's always the server side code in the end
that really decides what should happen in which scenario.
It is just worth pointing out
that because we typically use Post requests
for implying that data should be stored,
indeed Post requests are able to carry data
in their request body
whereas Get requests are not.
So, Get requests can't carry any data, Post requests can.
Now, as also mentioned before,
Get request is for example triggered,
if you enter a URL in the browser,
if you click a link or if you have a form
where the method is set to Get.
And for a Post request,
you'll only trigger that if the method is set to Post.
At least that was the case as long as we didn't know Ajax.
With Ajax, we can also send our own requests
from inside JavaScript,
and set the method to Get or not set any method at all
on the fetch function, since Get is the default there
as you learned, or set method to Post for Post requests.
So, these are the two HTTP methods we use all the time,
because if we leave aside Ajax,
if we ignore what we learned in this section,
then Get and Post were the only HTTP methods
the browser could use or send.
For form submission, you couldn't choose anything
besides Get or Post.
So Get or Post are the two main methods the browser knows.
However, with Ajax, that picture changes a little bit.
There you can, of course, also send Get and Post requests
as you learned in this course section,
but you can also send Put, Patch and Delete requests
in addition to that.
And these are brand new HTTP method types,
which we haven't seen before in this course,
which we also haven't used in this section,
but which exist in addition to Get and Post,
if you are using Ajax.
So, those JavaScript-driven HTTP requests
for sending requests.
Now, what do these methods mean?
And for which kind of requests would you typically use them?
Well, a Put request is typically used
if you want to imply that some data on the server
is updated or replaced.
And you would send it with the fetch function
by setting the method to Put.
A Patch request implies that data is updated.
So just like Put, but Put typically is used
if an entire resource, an entire document
in that database is replaced.
Whereas Patch is used if only parts of a resource,
parts of an element in a database are updated.
And you would send a Patch request
by setting the method to Patch.
The Delete method, as you might guess,
is used if you want to imply that some resource
is deleted, and you would send such a request
by setting the method to Delete.
So, these are brand new methods,
also sometimes called HTTP words,
which are available if you're using
these JavaScript-driven HTTP requests.
Now, this does not mean that you now have to use
JavaScript-driven requests for everything.
For example, in this demo application we worked on,
we do have delete and edit functionalities.
And if I click Delete Post here,
then we actually send a Post request,
not a Delete request,
because I sent this deletion request
with help of this,
here it is in post-item,
with help of this form that wraps this button.
And here I'm sending a Post request
because, as you learned, the browser,
if you rely on form submission, only knows Get or Post.
I can't choose Delete here.
This does not exist in the browser for form submissions.
Of course, we could now write JavaScript code
that prevents this form submission
and where we then send our own behind-the-scenes request,
where we do use the Delete method
to send such a delete request.
That would be possible.
We then just also have to handle a response appropriately.
And for example, manually remove the Delete as DOM element
from the DOM,
if that response tells us that the deletion was successful.
At the moment, that's not something we have to care about
because at the moment, for deleting,
I just redirect and therefore efficiently I reload the page.
That is not necessarily how we would handle it,
if we send such a behind-the-scenes request,
because there we typically handle the response ourselves
with our own JavaScript code.
Instead of letting the server redirect us somewhere.
You could do that,
but very often you would want to handle it yourself
in your own JavaScript code that runs in the browser.
And there you then might have code
for manually updating the DOM,
and manually removing the deleted item from there.
So, these are all things you can do,
but they're not mandatory.
I do just mention these extra HTTP methods here,
because we will encounter them later in the course again.
Of course when we do, I will, again,
briefly explain why we are using them then.
But you should already be aware of them.
Also, because of course you will see them
in more advanced web development resources
into which you might want to dive
once you're done with this course.
So, keep these extra methods in mind.
Keep in mind that they're only available
if you are working with JavaScript-driven HTTP requests.
And keep in mind that you would use them
for implying different actions
that should happen on the server,
but it's then still up to the server side code
to do the actual work.
You can send a Put request and just get back some data,
if the service side code
doesn't actually replace or update any resource.
So it's always the server side code
that does the actual work,
but if you were building your own website,
you'll of course want to write server-side code
that expects certain methods, that imply a certain message,
and then do the appropriate work.
And you then would also send the appropriate requests
from your client side,
so from your browser side code.
This already is a bit more advanced though,
and therefore it is something we will encounter again later.
As a last note, it is just worth knowing
that Put and Patch, just like Post,
can carry a request body.
Delete can't.
So, Delete works a lot like Get when it comes to that.