How do I make an HTTP request in Javascript?
- User
How do I make an HTTP request in Javascript?
- Assistant
To make an HTTP request in JavaScript, you can use the built-in
XMLHttpRequestobject or the more modernfetchAPI. Here's how you can make HTTP requests using both methods:Using
XMLHttpRequest:// Create a new XMLHttpRequest object const xhr = new XMLHttpRequest(); // Set the URL and HTTP method xhr.open('GET', 'https://api.example.com/data'); // Set request headers (optional) xhr.setRequestHeader('Content-Type', 'application/json'); // Send the request xhr.send(); // Handle the response xhr.onload = function() { if (xhr.status === 200) { // Request successful console.log(xhr.responseText); } else { // Request failed console.error('Request failed. Status:', xhr.status); } };
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.