I am new in web, I am serving an html when a button is clicked on the client side through a request, the body response of that request is the html. How can I retrieve the html or body response from the client side?
I am trying with this code but everything is empty:
JavaScript
x
8
1
var xhr = new XMLHttpRequest();
2
xhr.open(signedRequest.method, signedRequest.url, true);
3
console.log('xhr.response: ', xhr.response);
4
console.log('xhr.responseText: ', xhr.responseText);
5
console.log('xhr.responseXML: ', xhr.responseXML);
6
document.write('<p>xhr: ' + xhr + '</p>');
7
xhr.send();
8
Any idea on how to obtain the body response in the client-side?
Advertisement
Answer
Try to add a div or any input element with the id “demo” and try to run the code below.
JavaScript
1
9
1
var xhttp = new XMLHttpRequest();
2
xhttp.onreadystatechange = function() {
3
if (this.readyState == 4 && this.status == 200) {
4
document.getElementById("demo").innerHTML = "<strong>The response from the test URL is: </strong>" + this.responseText;
5
console.log(JSON.parse(this.response));
6
}
7
};
8
xhttp.open("GET", "https://httpbin.org/get", true);
9
xhttp.send();
JavaScript
1
1
1
<div id="demo"></div>