使用jquery中的开/关按钮读取thing speak频道上的数据?

使用jquery中的开/关按钮读取thing speak频道上的数据?,jquery,bootstrap-4,Jquery,Bootstrap 4,我有下面的代码,它做的开关。但是我现在希望能够在我的thingspeak频道上读取数据,使用读取频道url,当值为1且值为0时。我正在努力写这篇文章,请帮助我,谢谢 <div class = "wrapper" align = "center"> <div class="btn-group" id="toggle_event_editing"> <button type="button" class="btn btn-info locked_active"&

我有下面的代码,它做的开关。但是我现在希望能够在我的
thingspeak
频道上读取数据,使用读取频道url,当值为1且值为0时。我正在努力写这篇文章,请帮助我,谢谢

<div class = "wrapper" align = "center">
  <div class="btn-group" id="toggle_event_editing">
  <button type="button" class="btn btn-info locked_active">OFF</button>
 <button type="button" class="btn btn-default unlocked_inactive">ON</button>
</div> 
<div class="alert alert-info" id="switch_status">Switched off.</div>
 </div>

  $('#toggle_event_editing button').click(function(){
    if($(this).hasClass('locked_active') || $(this).hasClass('unlocked_inactive')){
        /* code to do when unlocking */
        $('#switch_status').html('Switched on.');
$.ajax({
    url:'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=1',
    type:'GET',
    data:{
    format:'text'
      },
    success:function(response){
    alert(response);
      },
    error:function(){
    $('#error').text("There was an error while processing your request.Please try again");
    $('#singlebtn').append(data);
      }

      });

      }

    }else{
        /* code to do when locking */
        $('#switch_status').html('Switched off.');
$.ajax({
    url:'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=0',
    type:'GET',
    data:{
    format:'text'
      },
    success:function(response){
    alert(response);
      },
    error:function(){
    $('#error').text("There was an error while processing your request.Please try again");
    $('#singlebtn').append(data);
      }

      });

      }
    }

    /* reverse locking status */
    $('#toggle_event_editing button').eq(0).toggleClass('locked_inactive locked_active btn-default btn-info');
    $('#toggle_event_editing button').eq(1).toggleClass('unlocked_inactive unlocked_active btn-info btn-default');
});

关
在…上
关掉。
$('#切换事件\编辑按钮')。单击(函数(){
if($(this).hasClass('locked_active')| |$(this).hasClass('unlocked_active')){
/*解锁时要执行的代码*/
$('switch#u status').html('switch on');
$.ajax({
网址:'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=1',
类型:'GET',
数据:{
格式:'text'
},
成功:功能(响应){
警报(响应);
},
错误:函数(){
$('#error').text(“处理您的请求时出错,请重试”);
$('#singlebtn')。追加(数据);
}
});
}
}否则{
/*锁定时要执行的代码*/
$('switch#u status').html('Switched off');
$.ajax({
网址:'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=0',
类型:'GET',
数据:{
格式:'text'
},
成功:功能(响应){
警报(响应);
},
错误:函数(){
$('#error').text(“处理您的请求时出错,请重试”);
$('#singlebtn')。追加(数据);
}
});
}
}
/*反向锁定状态*/
$(“#切换#事件#编辑按钮”).eq(0).toggleClass('locked#inactive locked#active btn default btn info');
$(“#切换#事件#编辑按钮”).eq(1).toggleClass('unlocked#unactive unlocked#active btn info btn default');
});

这将为您完成有关ajax的工作

$('#切换事件编辑按钮')。单击(函数(){
if($(this).hasClass('locked_active')){
/*解锁时要执行的代码*/
$('switch#u status').html('switch on');
$.ajax({
网址:'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=1',
类型:'GET',
数据:{
格式:'text'
},
成功:功能(响应){
警报(响应);
},
错误:函数(){
$('#error').text(“处理您的请求时出错,请重试”);
$('#singlebtn')。追加(数据);
}
});
}否则{
/*锁定时要执行的代码*/
$('switch#u status').html('Switched off');
$.ajax({
网址:'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=0',
类型:'GET',
数据:{
格式:'text'
},
成功:功能(响应){
警报(响应);
},
错误:函数(){
$('#error').text(“处理您的请求时出错,请重试”);
$('#singlebtn')。追加(数据);
}
});
}
/*反向锁定状态*/
$(“#切换#事件#编辑按钮”).eq(0).toggleClass('locked#inactive locked#active btn default btn info');
$(“#切换#事件#编辑按钮”).eq(1).toggleClass('unlocked#unactive unlocked#active btn info btn default');
});

关
在…上
关掉。

成功后,您是否收到ajax的回复?因为我不明白你有什么问题我的问题我想我的按钮做一个ajax调用我的频道,我没有得到任何成功,这是我的问题。这意味着,如果我的按钮是“在打开和关闭之间切换”,则必须更新我的频道,反之亦然。当我运行ajax时,我得到的响应是:7231,这部分代码是if($(this).hasClass('locked_active')|$(this).hasClass('unlocked_inactive')){如果打开或关闭,将始终发送&field8=1OFF@Ivan你能分享你的逻辑吗,将在我这边运行它作为一个测试,看看我是否得到了同样的行为。在stackoverflow这里,按F12->console,只粘贴ajax并点击enter,你将得到警报$.ajax({url:'',键入:'get',数据:{format:'text'}的响应,成功:函数(响应){alert(响应);},错误:函数(){$('#error').text(“处理请求时出错。请重试”);$('#singlebtn')。追加(数据);});非常感谢,伙计,我现在可以在thingspeak上看到我这边的请求了。我希望它只是一个使用引导开关在开和关之间切换的按钮。@user12443509这里是切换按钮的链接,你可以在w3schools.com/howto/howto_css_switch.asp上使用,如果答案正确,并且有助于你接受它来解决这个问题