简体   繁体   English

不引人注目的JavaScript方法,可从文本文件获取数据并将其写入HTML

[英]unobtrusive JavaScript method that gets data from text file and writes it to HTML

I posted a thread about this earlier and have made some progress but now my code isn't working and I am not too sure why. 我早些时候发布了一个与此相关的主题,并取得了一些进展,但是现在我的代码无法正常工作,我不太确定为什么。 I am attempting to use an unobtrusive JavaScript method that gets data from the provided text file (ajaxData.txt) and writes it to an element within the HTML. 我试图使用一种不引人注目的JavaScript方法,该方法从提供的文本文件(ajaxData.txt)中获取数据并将其写入HTML中的元素。 In this case, I am trying to write it to a button and have it display when the button is clicked. 在这种情况下,我试图将其写入按钮,并在单击按钮时显示它。

This is for a homework assignment and I am NOT allowed to use any type of JavaScript library :(. Here is my code: 这是一项家庭作业,我不允许使用任何类型的JavaScript库:(。这是我的代码:

<html>
<head>
<script type="text/javascript">

function loadXMLDoc(url){
    var xmlhttp;
    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById('A2').innerHTML=xmlhttp.statusText;
        }
    }

    xmlhttp.open("GET","ajaxData.txt",true);
    xmlhttp.send();
}

</script>
</head>
<body>

<h2>Retrieve data from XML file</h2>

<p><b>Status text:</b><span id="A2"></span></p>

<button onclick="loadXMLDoc('ajaxData.txt')">Get XML data</button>

</body>
</html>

I am still pretty new to JavaScript and especially new to AJAX so any type of help would be greatly appreciated!!! 我对JavaScript还是相当陌生的,尤其对AJAX还是陌生的,因此,非常感谢任何帮助!

Change... 更改...

xmlhttp.statusText

...to... ...至...

xmlhttp.responseText

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

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