0% found this document useful (0 votes)
26 views

HTTP Request in Javascript Free

The document discusses different methods for making HTTP requests in JavaScript, including the XMLHttpRequest object and the newer fetch API. The XMLHttpRequest object allows interacting with servers using HTTP/HTTPS but newer methods like fetch API are more convenient. Code examples are provided to demonstrate making requests using XMLHttpRequest and fetch API.

Uploaded by

dopinder
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

HTTP Request in Javascript Free

The document discusses different methods for making HTTP requests in JavaScript, including the XMLHttpRequest object and the newer fetch API. The XMLHttpRequest object allows interacting with servers using HTTP/HTTPS but newer methods like fetch API are more convenient. Code examples are provided to demonstrate making requests using XMLHttpRequest and fetch API.

Uploaded by

dopinder
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Javascript?

This object allows you to interact with servers using HTTP or HTTPS protocols. However, there are
newer and more convenient methods available, such as the fetch API, which I'll also include in the
examples.

This object allows you to interact with servers using HTTP or HTTPS protocols. However, there are
newer and more convenient methods available, such as the fetch API, which I'll also include in the
examples.

This object allows you to interact with servers using HTTP or HTTPS protocols. However, there are
newer and more convenient methods available, such as the fetch API, which I'll also include in the
examples.

This object allows you to interact with servers using HTTP or HTTPS protocols. However, there are
newer and more convenient methods available, such as the fetch API, which I'll also include in the
examples.

In JavaScript, you can make HTTP requests using various methods, but one of the most commonly
used methods is using the XMLHttpRequest object. This object allows you to interact with servers
using HTTP or HTTPS protocols. However, there are newer and more convenient methods available,
such as the fetch API, which I'll also include in the examples.

1. Using XMLHttpRequest (traditional approach):

// Create a new XMLHttpRequest object

var xhr = new XMLHttpRequest();

// Configure the HTTP method and URL

xhr.open('GET', 'https://api.example.com/data', true);

// Set up the event handler for successful response

xhr.onreadystatechange = function() {

if (xhr.readyState === 4 && xhr.status === 200) {

var responseData = JSON.parse(xhr.responseText);

console.log(responseData);

};

// Set up the event handler for errors (optional)

xhr.onerror = function() {
console.error('Error making the request.');

};

// Send the request

xhr.send();

You might also like