Professional Documents
Culture Documents
How Do I Make An HTTP Request in Javascript
How Do I Make An HTTP Request in Javascript
Using XMLHttpRequest:
javascript
Copy code
xhr.onload = function() {
};
xhr.onerror = function() {
// Handle error
};
xhr.send();
Using fetch:
javascript
Copy code
fetch('https://api.example.com/data')
.then(function(response) {
if (response.ok) {
return response.json();
})
.then(function(data) {
})
.catch(function(error) {
// Handle error
});
Both methods support various HTTP methods such as GET, POST, PUT, DELETE, etc. You can also add
headers and send data with the request if needed.