Answer the question
In order to leave comments, you need to log in
Arduino and ajax?
Good afternoon, I need the help of specialists, I can’t understand the principle by which data is exchanged using AJAX.
I understand how GET works, but I can't understand Ajax for the life of me... Please explain how the technology works.
I have a working example of receiving data from DS18B20 sensors, approximately I understand that the client (website) sends a request to the server if there is a part of the code in the GET request, then the function is executed:
A piece of the receiving code from client.read();
("GET /temp") != 0) { getTemp(client); }
GetTemp function output
void getTemp(EthernetClient client) {
sensors.requestTemperatures();
client.print(sensors.getTempCByIndex(0));
}
<script>
function getTemp() {
nocache = "&nocache=" + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("switch_txt").innerHTML = this.responseText;
}}}}
request.open("GET", "temp" + nocache, true);
request.send(null);
setTimeout('getTemp()', 1000);
}
</script>
<div id="switch_txt">00.00</div>
Answer the question
In order to leave comments, you need to log in
AJAX is from the word "asynchronous".
In one place in the code there is a request:
request.open("GET", "temp" + nocache, true);
request.send(null);
request.onreadystatechange = function() {
if (this.readyState == 4) { // тут и ниже просто проверка статусов, они разные могут быть
if (this.status == 200) {
if (this.responseText != null) {
// здесь ответ от сервера подставляется в HTML
document.getElementById("switch_txt").innerHTML = this.responseText;
}}}}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question