Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help fix ajax code

Status
Not open for further replies.

jtrembla

ISP
Joined
Jul 6, 2006
Messages
104
Location
US
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
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>&nbsp;</p>

Here is the xml file
Code:
<?xml version="1.0" ?>
<departments>
<department>Chemistry</department>  
</departments>
 
Just after taking a quick look, it appears you use "deparmentNodes" before it's declared - and I don't see a closing } for the first "for" loop.
Code:
function PopulateDeptList(departmentNode)
{
    var deptlist = document.getElementByTagName("departmentlist");
    // clear the deptartment list
    for (var count = deptlist.options.length-1; count >-1; count--)
    {
        [red]departmentNodes[/red].options[count] = null;
    

    var [red]departmentNodes[/red] = departmentNode.getElementsByTagName('department');

Adam
 
I corrected those mistakes and still receiving the same error, object expected at line 102. I presume i am reading the XML file wrong?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top