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
क्या यह लेख आपकी तत्काल जरूरतों को पूरा करता है? यदि हाँ, तो नीचे समीक्षा बॉक्स में हमें 5-स्टार रेटिंग छोड़ दें। हालांकि, यदि नहीं तो अपनी चिंता व्यक्त करने या एक प्रश्न पूछने के लिए टिप्पणी बॉक्स में एक टिप्पणी छोड़ दें , और हम जल्द से जल्द आपके पास वापस आ जाएंगे। सोशल मीडिया पर अपने दोस्तों के साथ इस लेख को साझा करना याद रखें !! ऐसा करने से हमें हमारे चैनल को बेहतर बनाने और बढ़ने में मदद मिलती है और हमें अधिक लोगों की ऑनलाइन । जानकारी के लिए धन्यवाद!!!
उत्तर