Javascript ESP8266网络服务器
我在stack overflow上发现了有趣的代码,但我唯一不喜欢的是它使用了通过Internet导入的JQuery,我需要它在不连接Internet的情况下工作。你能告诉我怎么改变吗 代码:Javascript ESP8266网络服务器,javascript,jquery,arduino,esp8266,Javascript,Jquery,Arduino,Esp8266,我在stack overflow上发现了有趣的代码,但我唯一不喜欢的是它使用了通过Internet导入的JQuery,我需要它在不连接Internet的情况下工作。你能告诉我怎么改变吗 代码: void handleRoot(){ snprintf(htmlResponse,3000, "\ \ \ \ \ \ \ 时间\ 嗯\ \ 拯救\ \ \ \ var-hh\ $(“#保存按钮”)。单击(函数(e){\ e、 预防默认值()\ hh=$('#date_hh').val();\ $
void handleRoot(){
snprintf(htmlResponse,3000,
"\
\
\
\
\
\
\
时间\
嗯\
\
拯救\
\
\
\
var-hh\
$(“#保存按钮”)。单击(函数(e){\
e、 预防默认值()\
hh=$('#date_hh').val();\
$.get('/save?hh='+hh,函数(数据){\
控制台日志(数据)\
});\
});\
\
\
");
server.send(200,“文本/html”,htmlResponse);
}
void handleSave(){
if(server.arg(“hh”)!=“”){
Serial.println(“小时数:+server.arg(“hh”));
}
}
无效设置(){
//启动序列号
序列号开始(115200);
延迟(10);
WiFi.begin(ssid,密码);
while(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
server.on(“/”,handleRoot);
server.on(“/save”,handleSave);
server.begin();
}
void循环(){
server.handleClient();
}
您只需将其作为脚本标记包含在计算机的本地路径中即可
<script src="path-to-jquery/jquery.min.js" type="text/javascript"></script>
编辑:首先,您必须下载所需的jquery文件并将其存储在本地路径中。jquery所需的路径应该是从html文件到jquery的相对路径。缩小的jquery javascript可以存储在ESP上,并在浏览器请求时由模块提供 一种简单的方法是使用提供HTML和jQueryJavaScript 这意味着在草图中的
data
子目录中创建index.html
。将原始草图中的HTML添加到文件中。还要将此文件中的脚本源更改为相对路径:
<script src="jquery.min.js"></script>
然后实现文件处理程序及其内容类型处理程序:
String getContentType(String filename)
{
if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".ico")) return "image/x-icon";
return "text/plain";
}
bool handleFileRead(String path) {
Serial.println("handleFileRead: " + path);
if (path.endsWith("/"))
{
path += "index.html";
}
String contentType = getContentType(path);
if (SPIFFS.exists(path))
{
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();
return true;
}
Serial.println("\tFile Not Found");
return false;
}
替代方法:删除JQuery依赖项
另一种方法是重写javascript,这样就不需要JQuery
这涉及到在按钮()上注册onclick处理程序,从输入字段()获取值并发送GET请求()这不起作用,因为您需要立即在ESP上加载JQuery,然后从那里以html格式打开它。您必须先下载JQuery文件并存储它(查看编辑的答案)。
SPIFFS.begin();
server.onNotFound([]() {
if (!handleFileRead(server.uri()))
server.send(404, "text/plain", "404: Not Found");
});
// retain the save endpoint
server.on("/save", handleSave);
server.begin();
String getContentType(String filename)
{
if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".ico")) return "image/x-icon";
return "text/plain";
}
bool handleFileRead(String path) {
Serial.println("handleFileRead: " + path);
if (path.endsWith("/"))
{
path += "index.html";
}
String contentType = getContentType(path);
if (SPIFFS.exists(path))
{
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();
return true;
}
Serial.println("\tFile Not Found");
return false;
}