Jump to content

[SOLVED] What is wrong with my code?


wazzoinc

Recommended Posts

Okay, I'm new at AJAX and have little experience with JavaScript (I'm familiar with Actionscript and am proficient in PHP w/ MySQL, just so you know I have a history of programming). For some reason, no matter what changes I make and what tests I run, nothing works! Essentially what I am trying to do is very basic; I want that when a page loads on this blog software I was creating, the posts are imported by AJAX. The particular usefulness of this is irrelevant, but below is the code for the XML, the HTML and the JavaScript pages:

 

HTML (with CSS styling, just in case it's part of the problem)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body,
html {
    margin:0;
    padding:0;
    background:#a7a09a;
    color:#000;
    }
body {
    min-width:750px;
    }
#wrap {
    background:#99c;
    margin:0 auto;
    width:750px;
    }
#header {
    background:#ddd;
    }
#nav {
    background:#c99;
    }
#posts {
    background:#9c9;
    }
#sidebar {
    background:#c9c;
    }
#footer {
    background:#cc9;
    }
#posts {
    background:#9c9;
    float:left;
    width:500px;
    }
#sidebar {
    background:#c9c;
    float:right;
    width:250px;
    }
#footer {
    background:#cc9;
/*clear:right;*/
    }
h3 {
padding-left:10px;
}
</style>
<script src="scripts/getPosts.js" language="javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body onload="javascript:do_xml()">
<div id="wrap">
    <div id="header"><h1>Document Heading</h1></div>
    <div id="nav">
        <ul>
            <li><a href="#" onclick="javascript:do_xml()">Option 1</a></li>
            <li><a href="#">Option 2</a></li>
            ...
        </ul>
    </div>
    <div id="posts">
    </div>
    </div>
    <div id="sidebar">
        <h3>Column 2</h3>
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p>
        <ul>
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            <li>Link 3</li>
            <li>Link 4</li>
            <li>Link5</li>
            <li>Link6</li>
            ...
        </ul>
    </div>

</div>
</body>
</html>

 

XML

<?xml version="1.0" encoding="utf-8"?>
<root>
<item>
	<title>Lorem Ipsum</title>
	<author>Jay</author>
	<date>12-12-12</date>
	<entry>       Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
       Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</entry>

</item>
<item>
	<title>Ipsum Lorem</title>
	<author>Jay</author>
	<date>12-12-12</date>
	<entry>       Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
       Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</entry>

</item>
</root>

 

AJAX/JavaScript

  var xmldoc = false;
   function makeRequest(url, parameters) {
  xmldoc = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         xmldoc = new XMLHttpRequest();
         if (xmldoc.overrideMimeType) {
            xmldoc.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            xmldoc = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               xmldoc = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      xmldoc.onreadystatechange = alertContents;
  xmldoc.async = false;
      xmldoc.load(url + parameters);
   }

   function alertContents() {
      if (xmldoc.readyState == 4) {
         if (xmldoc.status == 200) {
		//document.getElementById("posts").innerHTML = http_request.responseXML.xml;
		alert("why");
		var docelem = xmldoc.documentElement;
		if (docelem.firstChild.name == "error") {
			alert(docelem.firstChild.text);
			} else {
				var items = xmldoc.getElementsByTagName("item");
				var ititle = xmldoc.getElementsByTagName("title");
				alert(ititle[1].data);
				if (!items) {
					 alert('Cannot find item tag');
					 return false;
				} else {
					alert(items[0].childNodes.length);
					var title = [];
					var author = [];
					var date = [];
					var entry = [];
					//var children = Array("firstChild","firstChild.nextSibling","lastChild");

					for (var i = 0; i < 1; i++) {
							  title[i] = items[i].firstChild.data;
							  author[i] = items[i].firstChild.nextSibling.data;
							  date[i] = items[i].firstChild.nextSibling.nextSibling.data;
							  entry[i] = items[i].firstChild.nextSibling.nextSibling.nextSibling.data;
						  addpost(title[i], author[i], date[i], entry[i]);
					   }
				}
			}
            } else {
            alert('There was a problem with the request.' + '\nError ' + xmldoc.status);
         	}
         }
   }
   
   function do_xml() {
      makeRequest('test.xml', '');
   }
   
   function addpost(title, author, date, entry) {
   var pdiv = document.getElementById("posts");
   pdiv.innerHTML = "<h1>"+title+"</h1>\n";
   pdiv.innerHTML += "<h2>"+author+" : "+date+"</h2>\n";
   pdiv.innerHTML += "<h3>If you did not get this in email form and you think you're on the mailing list, please contact support</h3>\n";
   pdiv.innerHTML += "<p>"+entry+"</p>\n";
   }
   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.