Salve...
Ho fatto un esempio preso da un libro.... quando carico il file hml...ho il seguente errore:
"errore durante la lettura della risposta: Type error 'xmlresponse' non è definito. "
file html
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11.dtd">
<html>
<head>
<title>AJAX in pratica: Usare php dom </title>
<script type="text/javascript" src= "phptest.js"></script>
</head>
<body onload= "process()">
Il libro su AJAX del 2006 è:
<div id="myDivElement"/>
</body>
</html>
file phptest.js
codice:
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
var XmlHttpVersions = new array ("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
for (var i=0; i<XmlHttpVersions.length && !xmlHttp;i++)
{
try
{
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch(e){}
}
}
if (!xmlHttp)
alert("Errore durante la creazione dell'oggetto XMLHttpRequest.");
else
return xmlHttp;
}
function process()
{
if (xmlHttp)
{
try
{
xmlHttp.open("GET", "phptest.php",true);
xmlHttp.onreadystatechange =handleRequestStateChange;
xmlHttp.send(null);
}
catch (e)
{
alert("impossibile collegarsi al server:\n" + e.toString());
}
}
}
function handleRequestStateChange()
{
if(xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
try
{
handleServerResponse();
}
catch(e)
{
alert("errore durante la lettura della risposta:" + e.toString());
}
}
else
{
alert("C e stato un problema nella ricezione dei dati:\n"+xmlHttp.statusText);
}
}
}
function handleServerResponse()
{
var xmlResponse = xmlHttp.responseXML;
if (!xmlResponse || !xmlresponse.documentElement)
throw("Struttura XML non valida:\n" + xmlHttp.responseText);
var rootNodeName = xmlResponse.documentElement.nodeName;
if(rootNodeName=="parsererror") throw("Struttura XML non valida");
xmlRoot =xmlResponse.documentElement;
titleArray = xmlRoot.getElementsByTagName("title");
isbnArray = xmlRoot.getElementsByTagName("isbn");
var html ="";
for (var i=0; i<titleArray.length;i++)
html += titleArray.item(i).firstChild.data + ", "+ isbnArray.item(i).firstChild.data + "
";
myDiv = document.getElementById("myDivElement");
myDiv.innerHTML= html;
}
file php
codice:
<?php
header("Content-type: text/xml");
$dom= new DOMDocument();
$response =$dom->createElement('response');
$dom->appendChild($response);
$books = $dom->createElement('books');
$response->appendChild($books);
$title = $dom->createElement('title');
$titleText =$dom->createTextNode('Building Responsive Web Application with AJAX and PHP');
$title->appendChild($titleText);
$isbn=$dom->createElement('isbn');
$isbnText = $dom->createTextNode('1-904811-82-5');
$isbn->appendChild($isbnText);
$book= $dom->createElement('book');
$book->appendChild($title);
$book->appendChild($isbn);
$books->appendChild($book);
$xmlString=$dom->saveXML();
echo $xmlString;
?>