HTTP request in Javascript

There are several ways to an HTTP request in JavaScript, but two of the most commonly used methods are the XMLHttpRequest object and the fetch() function.

#1. Using XMLHttpRequest object:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
}
};
xhr.send();

#2. Using fetch() function:

fetch('https://example.com')
.then(function(response) {
return response.text();
})
.then(function(text) {
console.log(text);
});

Both examples above make a GET request to https://example.com and log the response to the console. Keep in mind that fetch is not supported by older browsers.

READ ALSO: How to redirect HTTP to HTTPS | Force HTTPS Using htaccess

Does this article meet your immediate needs? If yes, leave us a 5-star rating in the Review Box below. However, if not, leave a comment in the comment box to express your concern or ask a question, and we will get back to you as soon as possible.

Remember to Share this Article with your friends on social media!! Doing this helps us improve and grow our channel and also enables us to help more people online. 
Thank You For Sharing!!!

Discover more from Internet Parrot

Subscribe to get the latest posts sent to your email.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like
Headless WordPress

What is Headless WordPress and Should You Use It? (Pros & Cons)

Have you been wondering what headless WordPress is? Well, you are in…
Become a Content Manager

How to Become a Content Manager | Roles and Responsibilities of a Content Manager

Do you want to become a Content manager? These are the people…
Rank Math SEO

Rank Math SEO Solutions | How it Affects Your Website Ranking and SEO

What is the Best WordPress SEO Solutions For Your Business? As a…
Acceptable Ads

Revenue Optimization with Google AdSense – Acceptable Ads

If you are an AdSense Publisher, then you can gain more revenue…