简体   繁体   English

如何在客户端获取body response?

[英]How to obtain the body response in the client-side?

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?我是 web 的新手,当通过请求在客户端单击按钮时,我正在服务 html,该请求的正文响应是 html。如何从客户端检索 html 或正文响应?

I am trying with this code but everything is empty:我正在尝试使用这段代码,但一切都是空的:

 var xhr = new XMLHttpRequest();
 xhr.open(signedRequest.method, signedRequest.url, true);
 console.log('xhr.response: ', xhr.response);
 console.log('xhr.responseText: ', xhr.responseText);
 console.log('xhr.responseXML: ', xhr.responseXML);
 document.write('<p>xhr: ' + xhr + '</p>');
 xhr.send();

Any idea on how to obtain the body response in the client-side?关于如何在客户端获得身体反应的任何想法?

Try to add a div or any input element with the id "demo" and try to run the code below.尝试添加一个 div 或任何 ID 为“demo”的输入元素,并尝试运行下面的代码。

 var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = "<strong>The response from the test URL is: </strong>" + this.responseText; console.log(JSON.parse(this.response)); } }; xhttp.open("GET", "https://httpbin.org/get", true); xhttp.send();
 <div id="demo"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM