How to Make a HTTP Request in JavaScript? Last Updated : 22 May, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript has great modules and methods to make HTTP requests that can be used to send or receive data from a server-side resource. There are several approaches to making an HTTP request in JavaScript which are as follows: Table of Content Using fetch APIUsing AjaxUsing fetch APIThe JavaScript fetch() method is used to fetch resources from a server. It returns a Promise that resolves to the Response object representing the response to the request. The fetch method can also make HTTP requests- GET request (to get data) and POST request (to post data). Fetch also integrates advanced HTTP concepts such as CORS and other extensions to HTTP. Example: To demonstrate making an HTTP request in JavaScript using the fetch function. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <script> fetch("https://jsonplaceholder.typicode.com/todos", { method: "POST", body: JSON .stringify ({ userId: 1, title: "Demo Todo Data", completed: false, }), headers: { "Content-type": "application/json", }, }) .then((response) => response.json()) .then((json) => console.log(json)); </script> </body> </html> Output: OutputUsing AjaxAjax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and received using the HTTP GET method. It uses JSONPlaceholder, a free online REST API for developers that returns random data in JSON format. To make an HTTP call in Ajax, you need to initialize a new XMLHttpRequest() method, specify the URL endpoint and HTTP method (in this case GET). Finally, we use the open() method to tie the HTTP method and URL endpoint together and call the send() method to fire off the request. Example: To illustrates the use of XMLHttpRequest() object methods. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <script> const xhr = new XMLHttpRequest(); xhr .open("POST", "https://jsonplaceholder.typicode.com/todos"); xhr .setRequestHeader("Content-Type", "application/json"); const body = JSON .stringify( { userId: 1, title: "Demo Todo Data using XMLHttpRequest", completed: false, }); xhr .onload = () => { if (xhr.readyState == 4 && xhr.status == 201) { console.log(JSON.parse(xhr.responseText)); } else { console.log(`Error: ${xhr.status}`); } }; xhr.send(body); </script> </body> </html> Output: Output Comment More infoAdvertise with us Next Article How to Make a HTTP Request in JavaScript? K kamal1270be21 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Send an HTTP POST Request in JS? We are going to send an API HTTP POST request in JavaScript using fetch API. The FetchAPI is a built-in method that takes in one compulsory parameter: the endpoint (API URL). While the other parameters may not be necessary when making a GET request, they are very useful for the POST HTTP request. Th 2 min read How to make HTTP requests in Node ? In the world of REST API, making HTTP requests is the core functionality of modern technology. Many developers learn it when they land in a new environment. Various open-source libraries including NodeJS built-in HTTP and HTTPS modules can be used to make network requests from NodeJS.There are many 4 min read How Are Parameters Sent In An HTTP POST Request? HTTP POST requests are widely used in web development to send data from a client to a server. Whether you're submitting a form, uploading a file, or sending JSON data via an API, understanding how parameters are sent in an HTTP POST request is important. In this article, weâll explore how are parame 4 min read JavaScript Post Request Like a Form Submit Many web developers face the challenge of emulating the functionality of a form submission using a JavaScript POST request in situations where a form is not available or appropriate. The problem requires finding a way to replicate the behavior of a form submission using JavaScript, with the goal of 4 min read Make HTTP requests in Angular? In Angular, making HTTP requests involves communicating with a server to fetch or send data over the internet. It's like asking for information from a website or sending information to it. Angular provides a built-in module called HttpClientModule, which simplifies the process of making HTTP request 4 min read How HTTP POST requests work in Node ? The HTTP POST method is used to send data from the client to the server. Unlike GET, which appends data in the URL, POST sends data in the request body, which makes it ideal for form submissions, file uploads, and secure data transfers.In Node.js, handling POST requests is commonly done using the Ex 2 min read 4 Ways to Make an API Call in JavaScript API(Application Programming Interface) is a set of protocols, rules, and tools that allow different software applications to access allowed functionalities, and data, and interact with each other. API is a service created for user applications that request data or some functionality from servers.To 7 min read How to make ajax call from JavaScript ? Making an Ajax call from JavaScript means sending an asynchronous request to a server to fetch or send data without reloading the web page. This allows dynamic content updates, enhancing user experience by making the web application more interactive and responsive.There are multiple ways to make Aja 4 min read How to Intercept HTTP requests in web extension ? In this article, we will understand to intercept the HTTP requests made by the browser using web extension API.What is Intercepting HTTP Request?Intercepting HTTP Requests means manipulating the HTTP requests made by the browser through the user or through asynchronous event handlers. With the help 3 min read How to connect to an API in JavaScript ? An API or Application Programming Interface is an intermediary which carries request/response data between the endpoints of a channel. We can visualize an analogy of API to that of a waiter in a restaurant. A typical waiter in a restaurant would welcome you and ask for your order. He/She confirms th 5 min read Like