简体   繁体   English

如何使用 AJAX 通过网页向 ESP8266 发送值?

[英]How can I send a value to a ESP8266 through a webpage using AJAX?

I'm working with a NodeMCU ESP8266 and I want to control my WS2812B with it.我正在使用NodeMCU ESP8266 ,我想用它来控制我的WS2812B

So I made a HTML page with an input range and I want to send the value of the range to my ESP8266 - where the website is hosted- by using AJAX .所以我制作了一个带有输入范围的HTML页面,我想通过使用AJAX将范围的值发送到我的ESP8266 (网站所在的位置)。

I only found tutorials on how to send data from an ESP8266 to a webpage and can't find any tutorials on how to send any info to my ESP8266 from a webpage.我只找到了有关如何从ESP8266向网页发送数据的教程,找不到任何有关如何从网页向我的ESP8266发送任何信息的教程。

The input looks like this:输入如下所示:

<input type=range id="rangeinput">

To send a GET request from your webpage with the value you want to send, you can do something like this (untested, so check; I just typed it in, but you get the idea):要从您的网页发送带有您要发送的值的 GET 请求,您可以执行以下操作(未经测试,请检查;我只是输入了它,但您明白了):

var valueToSend = document.getElementById("rangeinput").value;

var ESP8266URL = ""; // URL of ESP8266 page that handles request goes here

var sendValueRequest = new XMLHttpRequest();
sendValueRequest.open("GET", ESP8266URL + "?value=" + valueToSend, true);
sendValueRequest.onreadystatechange = processReturn;
sendValueRequest.send(null);

function processReturn() {
  if (sendValueRequest.readyState == 4 && sendValueRequest.status == 200) {
    var return = sendValueRequest.responseText;
    // Do something (or nothing) with what the server sent back
  }
}

You will have to handle the GET request on the ESP8266.您必须在 ESP8266 上处理 GET 请求。 How to do that depends on how the webserver on your ESP8266 is set up.如何做到这一点取决于 ESP8266 上的网络服务器是如何设置的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM