![]() |
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 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.
Output: ![]() Output Using 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.
Output: ![]() Output |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |