Why is this code not working?
function sendHttprequest (uRL)
{
with ( new XMLHttpRequest ()){
open('get' ,uRL)
send ( void 0)
console . log ("RESPONSETEXT:",responseText);;
}
}
It's supposed to make an HTTPS get request to the specified url. Plz help!
i got this code from freecodecamp
You can’t access the response after making the XMLHttpRequest. You have to set an onload
handler to wait for the request to finish and then get the response when it loads. The usage of with
indicates outdated code and it’s not recommended.
So, your code, corrected:
function sendRequest(url) {
let request = new XMLHttpRequest();
request.onload = function (e) {
console.log('response', request.responseText);
};
request.open("GET", url);
request.send();
}
and if you must use with
:
function sendRequest(url) {
with (new XMLHttpRequest()) {
onload = function (e) {
console.log('response', responseText);
};
open("GET", url);
send();
}
}