5

How do I fix the first line in this ajax code that it doesnt show unexpected str...

 2 years ago
source link: https://www.codeproject.com/Questions/5318251/How-do-I-fix-the-first-line-in-this-ajax-code-that
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How can I fix the first line of the code that it doesnt display unexpected string and that it will load in a html file

JavaScript
Expand ▼   Copy Code
function loadxmldata('Q1.xml'){
	// Create the XML request
	xmlReq = null;
 
	// If Mozilla browser is used
	xmlReq = new XMLHttpRequest();
	
	if((xmlReq==null));
	{
		// Failed to create the request
		return;
	}
 
	//function to handle the request
	xmlReq.onreadystatechange = function(){
		switch(xmlReq.readyState){
		case 0: // Uninitialized
			break;
		case 1: // Loading
			break;
		case 2: // Loaded
			break;
		case 3: // Interactive
			break;
		case 4: // Complete
		// Retrieve the data 
		getXMLData(
        //Retrieves each child element in the XML file.
			xmlReq.responseXML.getElementsByTagName('number')
			[0].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('street')
			[0].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('town')
			[0].firstChild.data +
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('city')
			[0].firstChild.data +
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('code')
			[0].firstChild.data +
            "<hr>" +
			xmlReq.responseXML.getElementsByTagName('name')
			[1].firstChild.data + 
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('desc')
			[1].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('price')
			[1].firstChild.data +
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('quantity')
			[1].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('stockholders')
            [1].firstChild.data +
			"<hr>" +
			xmlReq.responseXML.getElementsByTagName('name')
			[2].firstChild.data + 
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('desc')
			[2].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('price')
			[2].firstChild.data +
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('quantity')
			[2].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('stockholders')
            [2].firstChild.data +
			"<hr>" +
			xmlReq.responseXML.getElementsByTagName('name')
			[3].firstChild.data + 
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('desc')
			[3].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('price')
			[3].firstChild.data +
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('quantity')
			[3].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('stockholders')
            [3].firstChild.data +
			"<hr>" +
			xmlReq.responseXML.getElementsByTagName('name')
			[4].firstChild.data + 
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('desc')
			[4].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('price')
			[4].firstChild.data +
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('quantity')
			[4].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('stockholders')
            [4].firstChild.data+
			"<hr>" +
			xmlReq.responseXML.getElementsByTagName('name')
			[5].firstChild.data + 
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('desc')
			[5].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('price')
			[5].firstChild.data +
			"<br>" +
			xmlReq.responseXML.getElementsByTagName('quantity')
			[5].firstChild.data + 
            "<br>" +
			xmlReq.responseXML.getElementsByTagName('stockholders')
            [5].firstChild.data);
			break;
			default:
			break;
        }
    }
 
		// Open and then send the request
		xmlReq.open ('GET', URL, true);
		xmlReq.send (null);
}


this is the html file that it needs to run in
Expand ▼   Copy Code
<!DOCTYPE html>
<html>
	<head>
		<title>
			Question 1 - HTML, AJAX, XML 
		</title>		
		<!-- Referencing the AJAX script file -->
		<script type="text/javascript" src="AJAX.js"></script>
		<script language="javascript">
			// This function is used to display the XML file
			function getXMLData(xmlFile){
				document.getElementById('display').innerHTML = xmlFile;
			}
		</script>
		<style>
			body{
				color:black;
				background-color:#F9C270;
				}
			h5{
				font-weight:bold;
				text-align:center;
				}
			header{
				font-weight:bold;
				font-size:56pt;
				text-align:center;
				font-family:Arial;
				color: #805500;
				}
			hr{
				outline-style:solid;               
				outline-color:white;               
				outline-width:5px;               
				outline-offset:5px;
				}
		</style>
	</head>
	<body onLoad="loadxmldata('Q1.xml')">
		<header>Bracken Candy Holders</header>
		<hr/>
		<p>
			<!-- The XML data will be displayed here -->
			<div id="display"></div>
		</p>
		<div><img src="smarties.jfif" id="smarties" height="150" width="150" id="smarties"/></div>
		<div><img src="jellytots.png" id="smarties" height="150" width="150" id="jellytots"/></div>
		<div><img src="jellybeans.jpg" id="smarties" height="150" width="150" id="jellybeans"/></div>
		<div><img src="sourstraw.jpg" id="smarties" height="150" width="150" id="sour straw"/></div>
		<div><img src="jawbreakers.jpg" id="smarties" height="150" width="150" id="jawbreakers"></div>
		<hr/>
		<footer>
			<h5>Copyrigth © Bracken Candy Holders</h5>
		</footer>
	</body>
</html>


What I have tried:

I have tried changing the ('Q1.xml') to (Q1.xml) then it will say unexpected token '.'
The code needs to work that it can display the xml file in html.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK