I'm using a NodeMCU to serve a webpage on which I have a button with which I would like to change a variable in my Arduino code. I can do this by adding this in my code:
server.on("/change", [](){
Serial.println("Change requested");
if (input_string != "A different text!") {
input_string = "A different text!";
}
else {
input_string = "Hello world!";
}
server.send(200, "text/plain", "Changing!");
});
I was hoping to be able jut to add a button to the HTML page that makes an Ajax call, as follows:
<button onclick="makeAjaxCall('change')">Change!</button>
but this gives me errors. I'm not sure if I'm missing something in order to be able to run the Ajax call, but it just doesn't work. What I can do is this:
<button onclick="window.location.href='/change'">Change?</button>
But of course, that redirects the whole page, which I don't want. I want the page to stay, not to refresh, or go anywhere else, just to pass data to the code.
Am I missing something in the code? Is there another way to do this?
but that causes the page to refresh,