简体   繁体   English

将文件中的文本显示到html网页中

[英]show text from file into html webpage

I am completely new in html and javascripts. 我是html和javascript的新手。 what i am trying to do is fairly simple i think. 我想做的是相当简单的。 I have a file hosted on a server and i want to show the text in the file on the page. 我有一个托管在服务器上的文件,我想在页面上的文件中显示文本。 I am following the answer for this Question . 我正在关注这个问题的答案。

However the text doesn't show at all. 但是,该文本完全不显示。

<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} just contains the url for the text file $ {summary}仅包含文本文件的网址

i've tried switching variable with the actual url no change 我试过切换与实际网址不变的变量

here's the url https://s3-us-west-1.amazonaws.com/mecturkaudiofiles/Order/20140707_tmm_pistorius_update_att.txt 这是网址https://s3-us-west-1.amazonaws.com/mecturkaudiofiles/Order/20140707_tmm_pistorius_update_att.txt

I don't have to do it using javascript i just want to find a way that works 我不必使用JavaScript来做,我只想找到一种可行的方法

Ps I've also tried PS我也试过了

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

this is what i get as an output 这就是我得到的输出 这是输出的预览

Add this before you .open() : .open()之前添加此.open()

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

and remove the whole xhr.onLoad line 并删除整个xhr.onLoad

EDIT: to populate the contents, instead of my console.log, use your: 编辑:要填充内容,而不是我的console.log,请使用:

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

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

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