/ Published in: JavaScript
This function is near enough the same as parsing XML. The only things that have changed are the sections marked !important.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function basicAJAX(file) {//pass a variable into the function var request = getHTTPObject(); if(request){ request.onreadystatechange = function() { parseResponse(request); }; request.open("GET", file, true);//this is where the var is picked up, the location request.send(null); } } function parseResponse(request) { if(request.readyState == 4){//waits for the complete before execute. if(request.status == 200 || request.status == 304){ var data = eval('('+request.responseText+')');//!Important<---------- createInfo(data); } else { alert("Something Broke!"); } } } function createInfo(data) { var holder = document.getElementById("showDiv");//the holder div while(holder.hasChildNodes()){ holder.removeChild(holder.lastChild); } //grab the info var name = data.person.name;//!Important<---------- var position = data.person.position;//!Important<---------- var email = data.person.email;//!Important<---------- var theUL = document.createElement("ul"); //name var nameLI = document.createElement("li"); var nameLIText = document.createTextNode(name); nameLI.appendChild(nameLIText); theUL.appendChild(nameLI); //position var positionLI = document.createElement("li"); var positionLIText = document.createTextNode(position); positionLI.appendChild(positionLIText); theUL.appendChild(positionLI); var emailLI = document.createElement("li"); var emailLIText = document.createTextNode(email); emailLI.appendChild(emailLIText); theUL.appendChild(emailLI); holder.appendChild(theUL); }