Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // There are many ways to make an HTTP request in JavaScript, but the most common way is using the XMLHttpRequest object. Here is an example of how to make a GET request to a server using XMLHttpRequest:
- var xhr = new XMLHttpRequest();
- xhr.open('GET', 'https://www.example.com/api/endpoint');
- xhr.send();
- To handle the response from the server, you can set up an event handler for the onload event of the XMLHttpRequest object:
- xhr.onload = function() {
- if (xhr.status === 200) {
- console.log('Success:', xhr.responseText);
- } else {
- console.error('Error:', xhr.statusText);
- }
- };
- // This will log the server's response to the console if the request was successful (HTTP status code 200) or log an error message if it was not.
- // There are also other ways to make HTTP requests in JavaScript, such as using the fetch function or third-party libraries like Axios or jQuery.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement