How do I make an HTTP request in Javascript ?

( Image : Pixabay | Image Link : Click Here )

In JavaScript, you can make an HTTP request using the built-in XMLHttpRequest object or the more modern fetch API. I’ll show you examples for both methods:

Using XMLHttpRequest:

javascript

var xhr = new XMLHttpRequest();
xhr.open(“GET”, “https://api.example.com/data”, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
// Process the response data here
console.log(response);
}
};
xhr.send();

This code sends a GET request to the specified URL (https://api.example.com/data). When the response is received and its status is 200 (OK), the onreadystatechange callback is triggered, and you can process the response.

Using the fetch API (more modern and recommended :

javascript

fetch(“https://api.example.com/data”)
.then(function(response) {
if (response.ok) {
return response.json();
}
throw new Error(“Network response was not ok.”);
})
.then(function(data) {
// Process the response data here
console.log(data);
})
.catch(function(error) {
// Handle any errors that occurred during the request
console.log(error);
});

The fetch function returns a promise that resolves to the Response object. You can use the ok property of the Response object to check if the request was successful. If it was, you can call the json() method on the Response object to parse the response data as JSON.

These examples demonstrate how to make a simple GET request, but you can also make other types of requests (e.g., POST, PUT, DELETE) by specifying additional parameters in the open method or the fetch function.

Posted in All

Leave a Reply

Your email address will not be published. Required fields are marked *

GLOTRU Footer
Popular Features
Popular Services/

Website Development & Design

App Development & Design

Graphic Design

Digital Marketing

SEO (Search Engine Optimization)

SMM (Social Media Marketing)

Cyber Security

Company

GLOTRU Founder & CEO : __Azam

Registared : Trade,MSME,etc

Board of Director

Team

About Us

Contact Us

Privacy Policy

Return & Refund Policy

Abuse Policy

Copyright Policy

Cookie Policy

Terms & Conditions

Universal Terms of Service

Disclaimer

Legal

Sponsorships

Investor

Press Releases

Our Investments

Brands

Newsroom

Business

...

_

Digital Millennium Copyright Act
DMCA.com Protection Status

_

Content similarity detection
Protected by Copyscape

_

***ANTI-PIRACY WARNING***

...................................................................................

Follow Us :

...................................................................................

SECURE SERVER : [Legal] [Privacy Policy] [Universal Terms of Service] [Do not sell my personal information]

SITE HOSTED : GLOTRU SECURE SERVER Asian Data Centre [You can host your site][Click Here]

SSL : Server Type : [Cloudflare] Certificate Issued By : [Let's Encrypt] Signature Algorithm : [ECDSA with SHA-384]

SITE BUILD SOFTWARE : Content Management System (CMS) Softwere

_

_

_