簡體   English   中英

將文件中的文本顯示到html網頁中

[英]show text from file into html webpage

我是html和javascript的新手。 我想做的是相當簡單的。 我有一個托管在服務器上的文件,我想在頁面上的文件中顯示文本。 我正在關注這個問題的答案。

但是,該文本完全不顯示。

<section>
<pre id="contents"></pre>
<script>function populatePre(url) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function () {
        document.getElementById('contents').textContent = this.responseText;
    };
    xhr.open('GET', url);
    xhr.send();
}
 populatePre(${summary}); 
</script>
</section>

$ {summary}僅包含文本文件的網址

我試過切換與實際網址不變的變量

這是網址https://s3-us-west-1.amazonaws.com/mecturkaudiofiles/Order/20140707_tmm_pistorius_update_att.txt

我不必使用JavaScript來做,我只想找到一種可行的方法

PS我也試過了

 <?php
echo file_get_contents("https://s3-us-west-1.amazonaws.com/mecturkaudiofiles/Order/20140707_tmm_pistorius_update_att.txt");
?>

這就是我得到的輸出 這是輸出的預覽

.open()之前添加此.open()

xhr.addEventListener('readystatechange', function(){
    if(xhr.readyState === 4 && xhr.status !== 0) {
    console.log(xhr.response)
};

並刪除整個xhr.onLoad

編輯:要填充內容,而不是我的console.log,請使用:

document.getElementById('contents').textContent = xhr.response;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM