function lightswitch()
{
    id = this.id.replace("light_", "");
    $.post("/lightswitch", { id: id })
        .fail(location.reload());
};

$(document).ready(function () {
    $(".toggle").each(function(index) {
        obj = $(".toggle")[index];
        document.querySelector('#' + obj.id).addEventListener('toggle', lightswitch, obj.id);
    });
});

function lightswitch()
{
    id = this.id.replace("light_", "");
    $.post("/lightswitch", { id: id });
};

$(document).ready(function () {
    $(".toggle").each(function(index) {
        obj = $(".toggle")[index];
        document.querySelector('#' + obj.id).addEventListener('toggle', lightswitch, obj.id);
    });
});

--- 
+++ 
@@ -1,7 +1,8 @@
 function lightswitch()
 {
     id = this.id.replace("light_", "");
-    $.post("/lightswitch", { id: id });
+    $.post("/lightswitch", { id: id })
+        .fail(location.reload());
 };
 
 $(document).ready(function () {
Handle failure to toggle light

Handle it by reloading the entire page. A little crude, sure, but it
does at least work.
