test1.html 3.87 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<title>Meine ersten Ajax Erfahrungen</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />

		<script type="text/javascript">
		<!--    
			function xmlToArray (xr)
			{
				ret = new Array ();

				if (! xr.hasChildNodes ())
					return ret;

				for (node in xr.childNodes)
				{
					if (node.nodeType == 1 && 
					    node.name == 'item')
					{
						key = childNode->getAttribute ('key');
						if (parseInt (key, 10) !== NaN)
							key = parseInt (key, 10);

						for (_node in node.childNodes)
						{
							if (_node->nodeType == 1)
							{
								switch (_node->nodeName)
								{
									case 'variable':	
										ret[key] = xmlToVar (_node);
										type = _node->getAttribute ('type');
										//settype (ret[key], type);
										break;

									case 'array':		
										ret[key] = xmlToArray (_node);
										break;

									case 'class':
										_class = _node->getAttribute ('class');
										eval ('ret[key] = new ' + _class + '(_node)');
								}
							}
						}
					}
				}

				return ret;
			}


			function xmlToVar (xr)
			{
				if (!xr.hasChildNodes ())
					return null;

				var ret = null;
				
				for (node in xr.childNodes)
					if (node.nodeType === 3 && node.nodeValue !== null)
					{
						ret = node.nodeValue;
						break;
					}

				return ret;
			}


			var req = null;
              
			function getPersons ()
			{            
				switch (req.readyState)
				{
					case 4:
						if (req.status != 200)
							alert("Fehler:"+req.status);
						else
						{    
							//for (x in req.responseXML)
							//{
						//		console.dir (x);
						//		break;
						//	}
							data = req.responseXML.firstChild;

							//console.dir (data);
							switch (data.nodeName)
							{
								case 'variable': ret = xmlToVar (data); break;
								//case 'array':    ret = xmlToArray (data); break;
								//case 'class':    ret = xmlToObject (data);
							}

							console.log ('%s', ret);

							// ----
							//german_resp = xml.getElementsByTagName("german")[0];
							//french_resp = xml.getElementsByTagName("french")[0];

							//schreiben des ergebnisses
							//german_field.value = german_resp.firstChild.nodeValue;
							//french_field.value = french_resp.firstChild.nodeValue;
							// ----

							// antwort des servers
							//var translation = eval('(' + req.responseText + ')');
                        
							// schreiben des ergebnisses
							//german_field.value = translation.german;
							//french_field.value = translation.french.val1;
						}
						return true;
            
					default:
						return false;
				}
			}
            
			function updPersons  ()
			{
				// erstellen des requests
                 
				try
				{
					req = new XMLHttpRequest();
				}
				catch (e)
				{
					try
					{
						req = new ActiveXObject("Msxml2.XMLHTTP");
					}
					catch (e)
					{
						try
						{
							req = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch (failed)
						{
							req = null;
						}
					}
				}

				if (req == null)
					alert("Error creating request object!");
                  
				// anfrage erstellen (GET, url ist localhost,
				// request ist asynchron      
				var url = 'http://localhost/~georg/bilder/ajax+json/ajax.php';

				req.open("GET", url, true);

				// Beim abschliessen des request wird diese Funktion ausgeführt
				req.onreadystatechange = getPersons;
	  
				req.setRequestHeader("Content-Type",
						"application/x-www-form-urlencoded");

				req.send(null);
			}

		//-->
		</script>
	</head>

	<body>
		<div id="eins" style="width: 600px; height: 450px; border: dashed 1px;">
			dummy: <input type="text" id="myword" onchange="updPersons ();" />
			<p id="persons">
				Hier koennen personen stehen....
			</p>
		</div>
	</body>
</html>