[solved] AJAX with Yun

I have an Yun, and i uploaded the "bridge" sketch into it.

inside the /www i created a "led" directory, the a index.html in the last one.

index.html

     <body background="imagens/caldeira.jpg">
                <form action=/arduino/digital/13 method=post>
                <p>
                <input name=Status type=text id=status value=status>
                </p>
                <p>&nbsp;</p>
                </form>
                <form action=/arduino/digital/13/1 method=post  enctype="application/x-www-form-urlencoded" target="_self">
                <p>
                <input name="Liga Caldeira" type=submit class="Azul" id="Liga Caldeira" value="Ligar Caldeira">
                </p>
                <p>&nbsp;</p>
                </form>
                <form action=/arduino/digital/13/0 method=post enctype="application/x-www-form-urlencoded" target="_self">
                <p>
                <input name="Desligar Caldeira" type=submit class="vermelho" id="Desligar Caldeira" value="Desligar Caldeira">
                </p>
                </form>
                <h1 class="cabe..alho"><span class="vermelho">Controlo Remoto da Caldeira de Aquecimento de ..gua</span></h1>
                <p>&nbsp;</p>
        </body>

Works perfectly!!

But the compiled bridge sketch generates another page to the browser with the updated value of the led 13 , and i didn't wanted that.

this function in the "bridge":

void digitalCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
  else {
    value = digitalRead(pin);
  }

  // Send feedback to client
  client.print(F("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.println(value);

  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}

To catch the value of this function without reloading the page, i need in java script , an "HttpRequest".

I want to retrieve this same updated value without reloading the whole page but the "status" value in the textbox of the first form, and i need a javascript code for it...

something like that..

AJAX portion ?

<span id="ajaxButton" style="cursor: pointer; text-decoration: underline">
  status
</span>
<script type="text/javascript">
(function() {
  var httpRequest;
  document.getElementById("ajaxButton").onclick = function() { makeRequest('/arduino/digital/13'); };

  function makeRequest(url) {
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
      }
    }

    if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.onreadystatechange = alertContents;
    httpRequest.open('GET', url);
    httpRequest.send();
  }

  function alertContents() {
    if (httpRequest.readyState === 4) {
      if (httpRequest.status === 200) {
        alert(httpRequest.responseText);
      } else {
        alert('There was a problem with the request.');
      }
    }
  }
})();
</script>

Does anybody can gave a heads up cause i put the AJAX SCRIPT in the index.html, but the error returns:
""There was a problem with the request."

i think, i not using right the span tag ... but ... i dont know, does anyone did something with the Bridge sketch and update a value without having change page, i mean reloads just the value without generating another page (default of the bridge)

In other words i meant to catch the "value" variable updated without having to reload the whole page...

Thank's in advance....
Rui Pereira

No hard code Ajax, Use JQuery, Zepto, Backbone, AngularJS instead of.

http://forum.arduino.cc/index.php?topic=217756.msg1591479#msg1591479

I have done two similar projects with the Yun and posted the code as examples. The is :Yun client server communication questions
This is a long post, but, the working code is the last reply. There is a example with AJAX (Zepto) and last is plain Javascript

The other is: Yun 4 by 20 web message board This uses /data/ page to transfer data using POST and GET. I hope this helps you.

Ps the servo sample might help me.. thx

http://www.fabiobiondi.com/blog/2014/02/html5-arduino-yun-and-angularjs-build-a-mobile-servo-controller/

..And thx for the "coloured" bridge..

ok, thank you both , i will take a look in to it..

:slight_smile: