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. 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:
<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!!!
Change…
…to…