I am new to ajax and I am trying to get this example to work. I get object expected on line 101. I have 2 files, ajax.html and an xml file names dept.xml. I think i need another set of eyes to help me. A star would of course be rewarded for your efforts.
Jen
Here is the ajax.html file
Here is the xml file
Jen
Here is the ajax.html file
Code:
<script type="text/javascript" language="javascript">
// create an instance of XMLHTTPRequest Object, varies with browser type, try for Mozilla then IE
function makeRequest(url) {
var http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function() { alertContents(http_request); };
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContents(http_request) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//var xmldoc = http_request.responseXML;
var root_node = xmldoc.getElementsByTagName('department').item(0);
//PopulateDeptList(http_request.responseXML.documentElement);
alert(root_node.firstChild.data);
} else {
alert('There was a problem with the request.');
}
}
}
// populate the contents of the department dropdown list
function PopulateDeptList(departmentNode)
{
var deptlist = document.getElementByTagName("departmentlist");
// clear the deptartment list
for (var count = deptlist.options.length-1; count >-1; count--)
{
departmentNodes.options[count] = null;
var departmentNodes = departmentNode.getElementsByTagName('department');
//var idValue;
var textValue;
var optionItem;
// populate the dropdown list with data from the xml doc
for (var count = 0; count < departmentNodes.length; count++)
{
textValue = GetInnerText(departmentNodes[count]);
//idValue = departmentNodes[count].getAttribute("id");
addOption(document.dept.departmentlist,”January”, “January”);
//optionItem = new Option( textValue, false, false);
//deptlist.options[deptlist.length] = optionItem;
}
}
// returns the node text value
function GetInnerText (node)
{
return (node.textContent || node.innerText || node.text) ;
}
function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
</script>
<p><span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest('dept.xml')">
Make a request
</span></p>
<FORM name="dept">
<SELECT NAME="departmentlist">
<Option value="" >Dept list</option>
</SELECT>
</form>
<p> </p>
Here is the xml file
Code:
<?xml version="1.0" ?>
<departments>
<department>Chemistry</department>
</departments>