Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 time
Hi, Here is the my first Ajax application , this application is so simple.The ajax application read an xml file from the server and load some data from it.
Here is the html file :
Code:
<html> <script type="text/javascript"> //read data from xml file function getXMLdata() { var request=null;
//Depends on the your browser if(window.XMLHttpRequest) { //Mozila FireFox request=new XMLHttpRequest(); } else if(window.ActiveXObject) { //Internet Explore request = new ActiveXObject("Microsoft.XMLHTTP"); }
if(request) { //The autocalled funtion when the status changed request.onreadystatechange = function() {
//Status cases of the readState varrible if(request.readyState==0) document.getElementById('status').innerHTML='Uninitialized'; else if(request.readyState==1) document.getElementById('status').innerHTML='Loading...'; else if(request.readyState==2) document.getElementById('status').innerHTML='Loaded'; else if(request.readyState==3) document.getElementById('status').innerHTML='Interactive'; else if(request.readyState==4) { document.getElementById('status').innerHTML='Completed'; var xml_response = request.responseXML; xml_response.loadXML(request.responseText); var myname=xml_response.getElementsByTagName('name'); document.getElementById('Txtdata').value=myname[0].childNodes[0].nodeValue; }
}
request.open("GET","Data.xml"); //send the request request.send(null);
} else //not request opened document.getElementById('Txtdata').value='Nothing'; }
</script> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- used for as a meta informaton to the browser --> <title>Untitled Document</title> </head>
In the beging i faced an problem when i tried to show the data. Every time i had to got " undefined " instead of the real data.The problem was in the following part of the code. document.getElementById('Txtdata').value=myname[0].childNodes[0].nodeValue; You have to take care where is your element postion in the xml file and which node you want to get data from ..
I post this code , just to make it easy for people to find the solution if they faced the same problem as i faced it . Any problem you can reply to my post .
Thanks alot ,
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )