javascriptajaxwindowswindows-desktop-gadgets

Windows 7 Gadget AJAX stopped working


I am writing a little Gadget that will show me the current status of my game server. It gets its information via an URL that returns JSON. At the beginning it worked fine. But now it seems to be stuck at the AJAX call.

This is my script:

function start() {
$("body").css({
    height: "100px",
    width: "130px",
    margin: 0
});

$("#refresh").click(function (event) {
    event.preventDefault();
    fetch_info();
});

fetch_info();
};

function fetch_info() {
var now = new Date();
var msec = now.getTime();
ajax = new ActiveXObject("Msxml2.XMLHTTP");
ajax.open("GET", "http://the.amazing.url?msec="+msec, true);
ajax.onreadystatechange = function() {
    if (ajax.readyState === 4) {
        if (ajax.status === 200) {
            var json = $.parseJSON(ajax.responseText);
            if (json.map != "error")
            {
                $("#map").html(json.map);
                $("#mode").html(json.gamemode);
                $("#current").html(json.players);
                $("#max").html(json.max_players);
            }
            else
            {
                $("#map").html("crashed");
            }
        } else {
            $("#map").html("error");
        }
    }
    else
    {
        $("#map").html("fetching data");
    }
}    
ajax.send(null);    
}

When the widget starts it is showing "fetching data" but after that it happens nothing. It gets valid JSON Data.

Does anyone know why or a way to debug this script?


Solution

  • There are two semicolon errors. They most likely occurred during your improvement of the script and the semicolon got moved. Line 14 does not need a semicolon, but Line 44 does.

    Line 14:

    13:       fetch_info();
    14:    }
    

    Line 44:

    44:    };
    45:    ajax.send(null);    
    46:    }
    

    As for Debugging Scripts, I use the built in browser developer tools Like F12 in IE or Developer Tools in Chrome. When I was making my Gadget, I tested all the scripts with a basic webpage so that I could watch the browser javascript console.

    JSLint is also helpful. However, I select the following options when using it: Assume Windows, Tolerate unused parameters, Tolerate missing 'use strict' pragma, Tolerate many var statements per function, and Tolerate messy white space. Else JSLint will go crazy on your script over things that do not matter. Hope this helps.

    Also if you have Visual Studio you can use its script debugger. Here is an article explaining how: http://www.howtogeek.com/howto/windows-vista/how-to-debug-a-windows-vista-sidebar-gadget-with-visual-studio/