Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. $("button").click(function(
  2. id = $(this).attr("id");
  3. toggle(id);
  4. ));
  5.  
  6. function toggle(id)
  7. {
  8. var username = "username";
  9. var password = "password";
  10.  
  11. $.ajax(
  12. {
  13. url:'http://myname.ddns.net:port/api/devices/' + id,
  14. type: 'get',
  15. dataType : 'json',
  16. contentType: 'application/json',
  17. async: true,
  18. beforeSend: function(xhr) {
  19. xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ':' + password));
  20. },
  21. success: function(data)
  22. {
  23. if(data.properties.value == "true")
  24. {
  25. $("#25").attr("src","../assets/images/icons/switch/off.png");
  26.  
  27. $.ajax(
  28. {
  29. url:"http://myname.ddns.net:91/api/devices/" + id +"/action/turnOff",
  30. type: 'post',
  31. dataType : 'json',
  32. contentType: 'application/json',
  33. async: true,
  34. beforeSend: function(xhr)
  35. {
  36. xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ':' + password));
  37. },
  38. })
  39. }
  40. else if(data.properties.value == "false")
  41. {
  42. $("#25").attr("src","../assets/images/icons/switch/on.png");
  43. $.ajax(
  44. {
  45. url:'http://myname.ddns.net:port/api/devices/"+id+"/action/turnOn',
  46. type: 'post',
  47. dataType: 'json',
  48. contentType: 'application/json',
  49. async: true,
  50. beforeSend: function(xhr)
  51. {
  52. xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ':' + password));
  53. },
  54. })
  55. }
  56. }
  57. },3000)
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement