Skip to main content

MAKE HTTP REQUEST IN Java Script | codes

 You can make an HTTP request in JavaScript using the XMLHttpRequest object or the fetch API.

Using XMLHttpRequest:

javascript
const xhr = new XMLHttpRequest(); xhr.open('GET', 'https://example.com/api/data', true); xhr.onload = function() { if (xhr.status === 200) { const response = JSON.parse(xhr.responseText); console.log(response); } }; xhr.onerror = function() { console.error('Request error'); }; xhr.send();

Using fetch:

javascript
fetch('https://example.com/api/data') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Fetch error:', error));

Both methods allow you to send requests and handle responses, but the fetch API is newer and more streamlined, while XMLHttpRequest is more versatile and supports features like aborting a request.

Comments

Popular posts from this blog

Download Radhe Movie (2021) 480p FilmyZilla Full HD, Tamilrockers

In the wake of taking the feared criminal Gani Bhai, ACP Rajveer Shikawat also known as Radhe goes on a manhunt to track down the most well off man of the town furtively running a criminal organization Radhe is an upcoming Indian Hindi-language action film directed by Prabhu Deva and produced by Salman Khan, Sohail Khan and Atul Agnihotri. A remake of 2017 Korean film The Outlaws, the film stars Salman Khan, Disha Patani, Randeep Hooda and Jackie Shroff. Wikipedia Release date: 13 May 2021 (India) Trending Director: Prabhu Deva Based on: The Outlaws; by Kang Yoon-sung Music director: Himesh Reshammiya, Sajid–Wajid, Sanchit Balhara, Devi Sri Prasad Producers: Salman Khan, Atul Agnihotri, Sohail Khan, Nikhil Namit Watch now Also read :- कोरोना के कारण फंगल संक्रमण How to download  Gta 5  Gta 5 download apk Gta 4 Download for pc नए कोविड स्ट्रेन लक्षण मूल से अलग कैसे हैं Karma Book by Sadhguru वैक्सीन के दोनों डोज लगे हों तो भी कोरोना हो सकता है Coronavirus nibandh in hindi ...

LEARN SOME BASIC JAVASCRIPT CODES

  Here are some basic JavaScript codes to get you started: Hello World: javascript Copy code console . log ( "Hello, World!" ); Variables and data types: javascript Copy code // Declaring variables var x = 5 ; let y = "Hello" ; const z = true ; // Data types console . log ( typeof x); // number console . log ( typeof y); // string console . log ( typeof z); // boolean Conditional statements: javascript Copy code let age = 20 ; if (age < 18 ) { console . log ( "You are not old enough to vote" ); } else if (age >= 18 && age <= 65 ) { console . log ( "You are eligible to vote" ); } else { console . log ( "You are above the voting age limit" ); } Loops: javascript Copy code // For loop for ( let i = 0 ; i < 5 ; i++) { console . log (i); } // While loop let j = 0 ; while (j < 5 ) { console . log (j); j++; } Functions: javascript Copy code // Function declaration function addNumber...