There are several ways to make an HTTP request in JavaScript, but two of the most commonly used methods are the XMLHttpRequest
object and the fetch()
function.
#1. Using XMLHttpRequest
object:
var xhr = new XMLHttpRequest(); xhr.open('GET', 'https://example.com', true); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { console.log(xhr.responseText); } }; xhr.send();
#2. Using fetch()
function:
fetch('https://example.com')
.then(function(response) {
return response.text();
})
.then(function(text) {
console.log(text);
});
Both examples above make a GET request to https://example.com and log the response to the console. Keep in mind that fetch is not supported by older browsers.
READ ALSO: How to redirect HTTP to HTTPS | Force HTTPS Using htaccess
Erfüllt dieser Artikel Ihre unmittelbaren Bedürfnisse? Wenn ja, hinterlassen Sie uns eine 5-Sterne-Bewertung im Überprüfungsfeld unten. Wenn nicht, hinterlassen Sie jedoch einen Kommentar im Kommentar, um Ihre Besorgnis auszudrücken oder eine Frage zu stellen, und wir werden uns so schnell wie möglich bei Ihnen melden. Denken Sie daran, diesen Artikel mit Ihren Freunden in den sozialen Medien zu teilen !! Menschen online zu helfen . Danke fürs Teilen !!!
Lassen Sie eine Antwort