There are 2 high level
ways a browser can send requests to an HTTP server:
method
attribute, which can be GET
or POST
. Forms with method="POST"
typically send data to the server for processing (e.g., form submissions).fetch
API. These requests can be of various types (GET
, POST
, PUT
, DELETE
, etc.) and are commonly used for asynchronous data retrieval and manipulation (e.g., AJAX requests).Server to send the request to - https://jsonplaceholder.typicode.com/posts/1 (GET request)
<!DOCTYPE html>
<html>
<body>
<div id="posts"></div>
<script>
async function fetchPosts() {
const res = await fetch("<https://jsonplaceholder.typicode.com/posts/1>");
const json = await res.json();
document.getElementById("posts").innerHTML = json.title;
}
fetchPosts();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="<https://cdnjs.cloudflare.com/ajax/libs/axios/1.7.6/axios.min.js>"></script>
</head>
<body>
<div id="posts"></div>
<script>
async function fetchPosts() {
const res = await axios.get("<https://jsonplaceholder.typicode.com/posts/1>");
document.getElementById("posts").innerHTML = res.data.title;
}
fetchPosts();
</script>
</body>
</html>