簡體   English   中英

不引人注目的JavaScript方法,可從文本文件獲取數據並將其寫入HTML

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

我早些時候發布了一個與此相關的主題,並取得了一些進展,但是現在我的代碼無法正常工作,我不太確定為什么。 我試圖使用一種不引人注目的JavaScript方法,該方法從提供的文本文件(ajaxData.txt)中獲取數據並將其寫入HTML中的元素。 在這種情況下,我試圖將其寫入按鈕,並在單擊按鈕時顯示它。

這是一項家庭作業,我不允許使用任何類型的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>

我對JavaScript還是相當陌生的,尤其對AJAX還是陌生的,因此,非常感謝任何幫助!

更改...

xmlhttp.statusText

...至...

xmlhttp.responseText

暫無
暫無

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

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