从服务器读取一个txt文件,写入网站 [英] Reading a txt file from Server, writing it to website

查看:1327
本文介绍了从服务器读取一个txt文件,写入网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题。
$ b 1)我想创建一个txt文件(或可能是一个html文件)来保存一段信息被写入当用户点击一个单词时。这是我正在开发的游戏的一部分。


如果你去www.w3schools.com,去他们的试用编辑器,他们有...

 <!DOCTYPE html> 
< html>
< head>
< script>
函数loadXMLDoc()
{
var xmlhttp = XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{
p = document .createElement( p);
p.innerHTML = xmlhttp.responseText;
document.getElementById(story)。appendChild(p);


xmlhttp.open(GET,ajax_info.txt,true);
xmlhttp.send();
}
< / script>
< / head>
< body>

< div id =story>< h2>让AJAX< i onclick = loadXMLDoc()>变更< / i>本文< / h2>< / div>

< / body>
< / html>

这正是我想让我的程序做的事情(当然不是真正的单词) m使用外部.js表单运行html。我应该把我的ajax存储在别的地方吗?或者,我的.txt文件应该看起来不仅仅是一堆文字而已?我完全困惑。

有趣的部分将会一旦实际开始工作,我将不得不实施一个onclick事件与斜体内的txt文件。

如果有更好的方法来做这个,那么appendChild(p.innerHTML(lfjhdfh))和AJAX请尽快告诉我。我总共有11个星期才能把这个东西用于学校项目LOL。

解决方案

试试这个,它只是附加文本到 body 元素的HTML:

  function getFileFromServer(url ,doneCallback){
var xhr;

xhr = new XMLHttpRequest();
xhr.onreadystatechange = handleStateChange();
xhr.open(GET,url,true);
xhr.send();

函数handleStateChange(){
if(xhr.readyState === 4){
doneCallback(xhr.status == 200?xhr.responseText:null);



$ b函数ajax(){
getFileFromServer(Test.txt,function(text){
if (text === null){
//发生错误
} else {
document.body.innerHTML + = test;
}
});

}


Here's my problem.

1) I'm wanting to create a txt file (or possibly a html file) to hold a paragraph of information to be written onto a when a user clicks a word. It's part of a game I am working on.

If you go to www.w3schools.com and go to their "try it" editor they have...

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp = XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
p = document.createElement("p");
p.innerHTML=xmlhttp.responseText;
document.getElementById("story").appendChild(p);
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="story"><h2>Let AJAX <i onclick=loadXMLDoc()>change</i> this text</h2></div>

</body>
</html>

This is exactly what I want my program to do (not the actual words of course) but I'm running html with an external .js sheet. Should I store my ajax somewhere else? Or, should my .txt file look different than just a bunch of words in it? I'm completely confused.

The fun part will be once this actually starts working, I'm going to have to implement an "onclick" event with italics inside of the txt file.

If there is a better way to do this then appendChild(p.innerHTML("lfjhdfh")) and AJAX please let me know asap. I have a total of 11 weeks to get this thing working for a school project LOL.

解决方案

Try this, it just appends the text to the HTML of the body element:

function getFileFromServer(url, doneCallback) {
    var xhr;

    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = handleStateChange();
    xhr.open("GET", url, true);
    xhr.send();

    function handleStateChange() {
        if (xhr.readyState === 4) {
            doneCallback(xhr.status == 200 ? xhr.responseText : null);
        }
    }
}

function ajax() {
    getFileFromServer("Test.txt", function (text) {
        if (text === null) {
            // An error occurred
        } else {
            document.body.innerHTML += test;
        }
    });

}

这篇关于从服务器读取一个txt文件,写入网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆