Georg Hopp 19 years ago
parent
commit
b9ef324a25
  1. 7
      ajax+json/ajax.php
  2. 21
      js/callbacks.js
  3. 5
      js/helper.js
  4. 48
      js/service.js
  5. 37
      js/xmlify.js
  6. 72
      testxmlify.html

7
ajax+json/ajax.php

@ -14,8 +14,9 @@ class dummy extends c_xmlify
protected $var1; protected $var1;
protected $var2; protected $var2;
protected $var3; protected $var3;
protected $var4;
function __construct ($xr = NULL, $v1, $v2, $v3)
function __construct ($xr = NULL, $v1, $v2, $v3, $v4)
{ {
if ($xr !== NULL) if ($xr !== NULL)
parent::__construct ($xr); parent::__construct ($xr);
@ -23,11 +24,13 @@ class dummy extends c_xmlify
$this->var1 = $v1; $this->var1 = $v1;
$this->var2 = $v2; $this->var2 = $v2;
$this->var3 = $v3; $this->var3 = $v3;
$this->var4 = $v4;
} }
}; };
$_data = new dummy ( $_data = new dummy (
NULL, array ('var1'=>0.53, 'var2'=>1.1), array (12, 13), "Hallo");
NULL, array ('var1'=>0.53, 'var2'=>1.1), array (12, 13), "Hallo",
new dummy (NULL, "Das", "ist", "ein", "test"));
//$_data = array (array ('var1'=>0.53, 'var2'=>1.1), array (12, 13), "Hallo"); //$_data = array (array ('var1'=>0.53, 'var2'=>1.1), array (12, 13), "Hallo");

21
js/callbacks.js

@ -0,0 +1,21 @@
if (typeof (nBildertool) === 'undefined')
nBildertool = {};
nBildertool.cbCommon = function ()
{
switch (nBildertool.req.readyState)
{
case 4:
if (nBildertool.req.status != 200)
alert ("Fehler:" + nBildertool.req.status);
else
{
data = nBildertool.deXmlify (nBildertool.req.responseXML);
console.dir (data);
}
return true;
default:
return false;
}
}

5
js/helper.js

@ -1,4 +1,7 @@
function extend (subClass, baseClass)
if (typeof (nBildertool) === 'undefined')
nBildertool = {};
nBildertool.extend = function (subClass, baseClass)
{ {
function inheritance() {} function inheritance() {}
inheritance.prototype = baseClass.prototype; inheritance.prototype = baseClass.prototype;

48
js/service.js

@ -0,0 +1,48 @@
if (typeof (nBildertool) === 'undefined')
nBildertool = {};
nBildertool.req = null;
nBildertool.service = function (url, callback)
{
// erstellen des requests
try
{
nBildertool.req = new XMLHttpRequest();
}
catch (e)
{
try
{
nBildertool.req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
nBildertool.req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
nBildertool.req = null;
}
}
}
if (nBildertool.req == null)
alert("Error creating request object!");
nBildertool.req.open("GET", url, true);
// Beim abschliessen des request wird diese Funktion ausgeführt
if (typeof (callback) === 'undefined')
nBildertool.req.onreadystatechange = nBildertool.cbCommon;
else
nBildertool.req.onreadystatechange = callback;
nBildertool.req.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
nBildertool.req.send(null);
}

37
js/xmlify.js

@ -1,4 +1,7 @@
function xmlToVar (data)
if (typeof (nBildertool) === 'undefined')
nBildertool = {};
nBildertool.xmlToVar = function (data)
{ {
if (!data.hasChildNodes ()) if (!data.hasChildNodes ())
return null; return null;
@ -25,7 +28,7 @@ function xmlToVar (data)
return ret; return ret;
} }
function xmlToObjectArray (data)
nBildertool.xmlToObjectArray = function (data)
{ {
var ret = new Array (); var ret = new Array ();
@ -43,16 +46,16 @@ function xmlToObjectArray (data)
switch (child.nodeName) switch (child.nodeName)
{ {
case 'variable': case 'variable':
ret[name] = xmlToVar (child); break;
ret[name] = nBildertool.xmlToVar (child); break;
case 'array': case 'array':
ret[name] = xmlToArray (child); break;
ret[name] = nBildertool.xmlToArray (child); break;
case 'class': case 'class':
var cName = child.getAttribute ('class'); var cName = child.getAttribute ('class');
if (typeof (eval (cName)) === 'function') if (typeof (eval (cName)) === 'function')
ret[name] = eval ("new " + cName + "(child)"); ret[name] = eval ("new " + cName + "(child)");
else else
ret[name] = xmlToObjectArray (child);
ret[name] = nBildertool.xmlToObjectArray (child);
} }
} }
} }
@ -60,7 +63,7 @@ function xmlToObjectArray (data)
return ret; return ret;
} }
function xmlToArray (data)
nBildertool.xmlToArray = function (data)
{ {
var ret = new Array (); var ret = new Array ();
@ -84,16 +87,16 @@ function xmlToArray (data)
switch (_child.nodeName) switch (_child.nodeName)
{ {
case 'variable': case 'variable':
ret[key] = xmlToVar (_child); break;
ret[key] = nBildertool.xmlToVar (_child); break;
case 'array': case 'array':
ret[key] = xmlToArray (_child); break;
ret[key] = nBildertool.xmlToArray (_child); break;
case 'class': case 'class':
var cName = child.getAttribute ('class'); var cName = child.getAttribute ('class');
if (typeof (eval (cName)) === 'function') if (typeof (eval (cName)) === 'function')
ret[key] = eval ("new " + cName + "(child)"); ret[key] = eval ("new " + cName + "(child)");
else else
ret[key] = xmlToObjectArray (child);
ret[key] = nBildertool.xmlToObjectArray (child);
} }
} }
} }
@ -103,7 +106,7 @@ function xmlToArray (data)
return ret; return ret;
} }
function c_xmlify (data)
nBildertool.c_xmlify = function (data)
{ {
if (! data.hasChildNodes ()) if (! data.hasChildNodes ())
return; return;
@ -121,23 +124,23 @@ function c_xmlify (data)
switch (child.nodeName) switch (child.nodeName)
{ {
case 'variable': case 'variable':
this[name] = xmlToVar (child); break;
this[name] = nBildertool.xmlToVar (child); break;
case 'array': case 'array':
this[name] = xmlToArray (child); break;
this[name] = nBildertool.xmlToArray (child); break;
case 'class': case 'class':
var cName = child.getAttribute ('class'); var cName = child.getAttribute ('class');
if (typeof (eval (cName)) === 'function') if (typeof (eval (cName)) === 'function')
this[name] = eval ("new " + cName + "(child)"); this[name] = eval ("new " + cName + "(child)");
else else
this[name] = xmlToObjectArray (child);
this[name] = nBildertool.xmlToObjectArray (child);
} }
} }
} }
} }
} }
function deXmlify (data)
nBildertool.deXmlify = function (data)
{ {
var ret = null; var ret = null;
var child = data.firstChild; var child = data.firstChild;
@ -145,16 +148,16 @@ function deXmlify (data)
switch (child.nodeName) switch (child.nodeName)
{ {
case 'variable': case 'variable':
ret = xmlToVar (child); break;
ret = nBildertool.xmlToVar (child); break;
case 'array': case 'array':
ret = xmlToArray (child); break;
ret = nBildertool.xmlToArray (child); break;
case 'class': case 'class':
var cName = child.getAttribute ('class'); var cName = child.getAttribute ('class');
if (typeof (eval (cName)) === 'function') if (typeof (eval (cName)) === 'function')
ret = eval ("new " + cName + "(child)"); ret = eval ("new " + cName + "(child)");
else else
ret = xmlToObjectArray (child);
ret = nBildertool.xmlToObjectArray (child);
} }
return ret; return ret;

72
testxmlify.html

@ -7,12 +7,11 @@
<script src="js/helper.js" type="text/javascript"></script> <script src="js/helper.js" type="text/javascript"></script>
<script src="js/xmlify.js" type="text/javascript"></script> <script src="js/xmlify.js" type="text/javascript"></script>
<script src="js/service.js" type="text/javascript"></script>
<script src="js/callbacks.js" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
//<![CDATA[ //<![CDATA[
var req = null;
var data;
function dummy (data) function dummy (data)
{ {
this.var1 = null; this.var1 = null;
@ -22,69 +21,7 @@
dummy.baseConstructor.call (this, data); dummy.baseConstructor.call (this, data);
} }
extend (dummy, c_xmlify);
function getData ()
{
switch (req.readyState)
{
case 4:
if (req.status != 200)
alert ("Fehler:" + req.status);
else
data = deXmlify (req.responseXML);
console.dir (data);
return true;
default:
return false;
}
}
function updData ()
{
// 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 = getData;
req.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
req.send(null);
}
nBildertool.extend (dummy, nBildertool.c_xmlify);
//]]> //]]>
</script> </script>
</head> </head>
@ -92,7 +29,8 @@
<body> <body>
<script language="javascript"> <script language="javascript">
//<![CDATA[ //<![CDATA[
updData ();
var serviceUrl = 'http://localhost/~georg/bilder/ajax+json/ajax.php'
nBildertool.service (serviceUrl);
console.log ('%s', 'jokus'); console.log ('%s', 'jokus');
//]]> //]]>
</script> </script>

Loading…
Cancel
Save