大家好,我可能会离开,所以请告诉我是否有更好的方法,但我有一些 tasmoto sooff 设备,我在按钮上有这个代码链接可以打开和关闭灯
http://192.168.1.126/cm?cmnd=Power%20toggle'" />
现在,当我这样做时,我会从命令中获得输出,我想我不介意将输出作为一种状态来了解灯正在做什么,因为我也想显示它,但是输出显示在不同的页面上,所以我还不能在屏幕上将它用作按钮
HTML 代码我知道它是错误的,我该如何修复它,请
<html>
<body>
<form>
<input class="MyButton" type="button" value="Office Toggle" onclick="window.location.href='http://192.168.1.126/cm?cmnd=Power%20toggle'" />
<br>
<input class="MyButton" type="button" value="Fishtank Toggle" onclick="window.location.href='http://192.168.1.128/cm?cmnd=Power%20toggle'" />
</form>
}
</body>
function togglelight() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText); //To check output while error[Optional]
}
};
xhttp.open("GET", "http://192.168.1.126/cm?cmnd=Power%20toggle", true);
xhttp.send();
</html>
所以你想运行一个链接/页面而不留下显示其结果。为什么不看看XMLHttpRequest。您可以在不离开原始页面的情况下进行请求。这是一个例子。
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
// check the response status/state
if (this.readyState == 4 && this.status == 200) {
//Create a response action here
}
};
// create a request
xhttp.open("GET", "http://192.168.1.126/cm?cmnd=Power%20toggle", true);
xhttp.send();
希望这对你有帮助。
您的代码将如下所示
function togglelight() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText); //To check output while error[Optional]
}
};
xhttp.open("GET", "http://192.168.1.126/cm?cmnd=Power%20toggle", true);
xhttp.send();
}
<html>
<body>
<input class="MyButton" type="button" value="Office Toggle" onclick="togglelight()" />
<br>
<input class="MyButton" type="button" value="Fishtank Toggle" onclick="togglelight()" />
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句