簡體   English   中英

如何在客戶端獲取body response?

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

我是 web 的新手,當通過請求在客戶端單擊按鈕時,我正在服務 html,該請求的正文響應是 html。如何從客戶端檢索 html 或正文響應?

我正在嘗試使用這段代碼,但一切都是空的:

 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();

關於如何在客戶端獲得身體反應的任何想法?

嘗試添加一個 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