• Mobile
  • Social
  • IM
  • Computer
  • Internet
  • Smarthome
  • Travel
  • Mobile
  • Social
  • IM
  • Computer
  • Internet
  • Smarthome
  • Travel
home/knowledge base/Internet/How do I make an HTTP request in Javascript?
Popular search:facebook, android, Excel

How do I make an HTTP request in Javascript?

Napisane przez guru
January 3, 2023

To make an HTTP request in JavaScript, you can use the XMLHttpRequest object or the newer fetch API. Here’s an example using XMLHttpRequest:

function makeRequest(method, url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}

makeRequest('GET', 'https://example.com')
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});

Here’s an example using the fetch API:

fetch('https://example.com')
.then(response => response.text())
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});

Both examples make a GET request to the specified URL and log the response to the console. The fetch API is generally easier to use and has better support for streaming and handling errors, but XMLHttpRequest is still widely used and supported.

Tags:javascript

Was it helpful?

Yes  No
Related Articles
  • Clear browsing history in Chrome
  • How to check your password – Google Chrome
  • How to use google maps to find an interresting place to visit?
  • Google Maps language
  • How to change the Facebook language
  • How to set-up Chrome as a default browser in Windows 10
Leave A Comment Cancel reply

  • English
Popular Articles
  • Facebook lock – how long?
  • How do I mark important places on Google Maps?
  • The cleaning robot gets stuck under the furniture – the solution
  • How to make a table of contents in Word?
  • How to take a screenshot
KB Categories
  • Mobile
  • Social
  • Computer
  • Internet
  • Smarthome
  • Travel
  • IM
  • android
  • Facebook
  • messenger
  • WhatsApp
  • Office
  • Other
  • iOS

Try To Help ME

Welcome to the website that's here to turn you from a tech-turtle to a tech-ninja! We've got all the information and instructions you need to navigate the wild world of new technologies, with a side of humour to make it less overwhelming.

Whether you're struggling with a stubborn smartphone or trying to figure out how to use that fancy new app, we've got your back (and your funny bone). So come on in, and let's get tech-savvy together!

Tags
#techsolutions #mobilesupport #socialmediamanagement #computersupport #technologytips #mobileappdevelopment #socialmediastrategies #computerrepair #techtroubleshooting #mobilenetworksolutions
Social Media
#socialmedia #socialmediamarketing #instagram #facebook #twitter #linkedin #socialmediahacks #socialmediamanagement #socialmediastrategies #socialmediatips
Social
  • Facebook
  • Twitter
  • Privacy Policy
  • Terms of Use
  • © 2020 naszanasze. All Rights Reserved.

Popular search:facebook, android, Excel
  • English