Javascript - Read Json from esp8266 SPIFFS



起初我想说我在Javascript/jQuery方面的经验为零,我知道我的问题的解决方案实际上可能很简单。

我想用我的ESP8266制作一个WiFi扫描仪,在页面上的表格中显示SSID。下面的代码到目前为止可以工作,但延迟非常混乱。我必须可以检查 while 循环是否实际填充了 json 文件,如果是这样,请阅读它并显示 ssid。

var foot = '<tr style="height:63px;"><td>Selected WiFi: ... &nbsp;Password:&nbsp;<input id="pw" type="text"><button onClick="connect()" class="btn btn-primary" type="button" style="margin-left:5%;">Connect</button></td></tr>';
var foot_last = '<tr style="height:63px;"><td>Selected WiFi: ... &nbsp;Password:&nbsp;<input id="pw" type="text"><button onClick="connect()" class="btn btn-primary" type="button" style="margin-left:5%;">Connect</button></td></tr>';
//gets called when the scan button is pressed
function scan() {
//Tells the ESP8266 to write the SSIDs in the json file scan.json (takes a while until its done)
$.post("/scan");
//this delay needs to be replaced. The code should only move on if the scan.json is not empty
delay(2000);
//Json gets read and contents displayed 
$.getJSON("/scan.json", function(data) {
$.each(data, function(index, value) {
console.log(value);
var tbl_hoe = '<tr onClick="select()" style="height:63px;"><td>' + value + '</td></tr>';
foot = tbl_hoe + foot;
document.getElementById('tab').innerHTML = foot;
});
});
//at this point the json must be emtyed
}
function delay(ms) {
ms += new Date().getTime();
while (new Date() < ms) {}
}

关于//this delay needs to be replaced. The code should only move on if the scan.json is not empty
如果您查看 $.post 的文档,您会发现它需要 2 个参数。第一个是 url,第二个是请求完成后调用的回调。

function scan() {
$.post("/scan", function(data) {
$.getJSON("/scan.json", function(data) {
$.each(data, function(index, value) {
console.log(value);
});
});
});
}

关于://at this point the json must be emtyed
除非您向服务器发出请求,否则您无法从浏览器更改 json 文件的内容。(并且服务器必须已经实现了这样的功能(

大坦克给戴夫牛顿。我没有想到我可以使用ESP8266直接通过http发送数据,而无需中间的json

最新更新