I am currently learning about web development and came across HTTP. One thing that is confusing me is how exactly to make HTTP requests. In the videos I’ve watched the main way they send HTTP requests are through Form Elements. Doing a little bit more research, I also found out that HTTP requests can be done manually via Fetch and XMLHttpRequest.
So I’m a bit confused on how all 3 of these work and the benefits of choosing one over the other. I do apologize if I make any mistakes, but I’m largely confused on the entire matter. I tried manually sending HTTP Requests like PUT and POST with Fetch, but that was kind of a mess. Most tutorials were using Form Elements to do so, so now I’m left wondering if that’s just how it should be done.
I also read over Sending forms through JavaScript but it completely went over my head.
So I tried looking up various tutorials and websites to explain the matter of HTTP requests with HTML/CSS & Javascript. I’m hoping that someone can explain the differences between Forms, Fetch, and XMLHttpRequest. And what are the tradeoffs of using one over the other if that is applicable in this case.
2
Answers
Forms:
XMLHttpRequest:
Fetch API:
Tradeoffs and Recommendations:
In summary, Fetch is often the preferred choice for making HTTP requests in modern web development due to its simplicity and power, but understanding Forms and XMLHttpRequest can be beneficial, especially when dealing with existing code or specific use cases.
I’m no expert on this topic but there are different use cases for
Form
,fetch
andXMLHttpRequest
fetch
is preferred toXMLHttpRequest
because it is newer and allows a more modern interface to perform requests. In comparison to forms, you can choose the body/request content through JavaScript.TLDR; Use
fetch
if you want a modern api, useXMLHttpRequest
if you need an older api, and use a form element if you want to send data from a series of html elements