Go Down

Topic: Ajax and XML (Read 457 times) previous topic - next topic

luigialab

Hi, i was trying to use XML , Ajax to input data on my server Web (made by an Arduino Ethernet) but i have some problems.
I followed this giude http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-AJAX-XML-web-server/ but i've made some changes.
This is my code:
Arduino (only the function that interacts with Ajax and XML):
Code: [Select]

client.println("HTTP/1.1 200 OK");
          if (inString.indexOf("came.htm") >0){
                  // send rest of HTTP header
                        client.println("Content-Type: text/xml");
                        client.println("Connection: keep-alive");
                        client.println();
                        Serial.println("Sono passato di qui.");
                        // send XML file containing input states
             xmlResponse(client);
           }


Code: [Select]
void xmlResponse(EthernetClient cl)
{
    cl.print("<?xml version = \"1.0\" ?>");
    cl.print("<inputs>");
    cl.print("<button1>");
    if (digitalRead(7)) {
        cl.print("ON");
    }
    else {
        cl.print("OFF");
    }
    cl.print("</button1>");
    cl.print("<button2>");
    if (digitalRead(8)) {
        cl.print("ON");
    }
    else {
        cl.print("OFF");
    }
    cl.print("</button2>");
    cl.print("</inputs>");
}


HTML and AJAX function:
Code: [Select]
<html>
<head>
<meta name='apple-mobile-web-app-capable' content='yes' />
<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />
<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />
<script type="type/javascript">
function ajaxRequest(){
var xmlhttp = new XMLHttpRequst;

xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
/*document.getElementById("temp").innerHTML = "";
document.getElementById("temp").innerHTML = xmlhttp.responseText;
*/
 // extract XML data from XML file (containing switch states and analog value)
                            document.getElementById("input1").innerHTML =
                                this.responseXML.getElementsByTagName('button1')[0].childNodes[0].nodeValue;
                            document.getElementById("input2").innerHTML =
                                this.responseXML.getElementsByTagName('button2')[0].childNodes[0].nodeValue;
}
}
xmlhttp.open("GET","http://192.168.0.254/came",true);
xmlhttp.send();
setTimeout('ajaxRequest()',1000);
}
</script>
</head>
<body onload="ajaxRequest()">
<br>
<br
<span id="input1"></span>
<br>
<span id="input2"></span>
<br>
<span id="input3"></span>
<br>
</body>
</html>


But when i access to http://myip/came.htm my browser starts loading the page endlessly. Could anyone help me? Please, thanks.

Go Up