4

I am trying to access data stored in a JSON file (in the same folder as the gadget) using jquery. The following example works fine in both firefox and internet explorer (shows "success"), but as a gadget it doesn't work (shows "fail").

$('#gadgetContent').html("fail");

$.getJSON("test.json", function(data) {

    $('#gadgetContent').html("success");
});

Any ideas as to what I'm doing wrong? Thanks.

UPDATE:

$.ajax({
    url: "test.json",
    dataType: 'json',
    error: jsonError,
    success: jsonSuccess
});

function jsonError(jqXHR, textStatus, errorThrown) {

    // As a gadget this function is called
    // jqXHR.readyState is 4
    // jqXHR.status is 0
    // jqXHR.responseText is undefined
}

function jsonSuccess(data) {
    // Browsers reach here
}
grc
  • 22,885
  • 5
  • 42
  • 63
  • What do you mean by 'local JSON file'? Do you mean it exists on the server, or are you trying to load it from the OS... – Blazes Jun 13 '11 at 08:15
  • It's in the same folder as the windows gadget – grc Jun 13 '11 at 08:18
  • Try giving the full path to the json file and see if it works. – Rakesh Sankar Jun 15 '11 at 07:49
  • Thanks, but that doesn't seem to make a difference. – grc Jun 15 '11 at 09:14
  • 1
    Although I cannot speak with absolute certainty, I suspect that the windows desktop gadget lacks support for the .json file extension. Perhaps try using .js instead? Might need to refactor a bit, such as using `getScript` (http://api.jquery.com/jQuery.getScript/) and having your data set to a variable. – Adam Terlson Jun 15 '11 at 18:29
  • `getScript` doesn't work either. But you reminded me of the script tag, which works (with the data as a variable). I'll probably go with that, but if anyone knows a better solution I'd be keen to hear it. – grc Jun 16 '11 at 09:07
  • I wasn't intending to say use "getScript" with your current .json implementation. It should be used for a .js file which has that variable. I'm going to repost as an answer so that others may find it. I hope that if this was helpful to you, you'll mark it as the answer. – Adam Terlson Jun 16 '11 at 12:53

3 Answers3

5

You should read the file like text and then convert it to json. This utility should help you:

    function getJsonFromFile(fileName) {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        if (fso.FileExists(fileName)) {
                var f = fso.OpenTextFile(fileName, 1);
                var jsonStr = "";
                while (!f.AtEndOfStream) {
                    jsonStr += f.ReadLine();
                }
                f.Close();
        }

        return jQuery.parseJSON(jsonStr);
    }

Remember to call it with full path like:

var gadgetPath = System.Gadget.path;
var jsonFile = gadgetPath + "\\" + "foo.json";

var json = getJsonFromFile(jsonFile);
ShashiKant
  • 210
  • 2
  • 8
  • Thanks! It works great, although I have to add `jsonStr = jsonStr.substring(3)` before parsing because for some reason there are 3 random characters at the beginning of `jsonStr`. – grc Jun 21 '11 at 07:43
  • shouldn't you edit your json file to remove the invalid characters? – ShashiKant Jun 23 '11 at 01:47
  • There aren't any invalid characters in the json file. – grc Jun 23 '11 at 06:00
1

The windows gadgets security sandbox restrictions, will be interfering with the way ajax works. When you pass a url to ajax call, it trid to make an HTTP request to that url, and incase of a browser the url does exist in the form of (file://localpath), but with a windows gadgets things are a bit different, i.e the relative url which is derived from window.location cannot be used as the window object does not exist here.

The easiest here would be to simply put the json in a JS file and refer it using the script tag, as that part of the HTML DOM is taken care of by the sidebar.exe code which takes care of rendering/loading stuff.

Thanks

Neeraj

Neeraj
  • 8,408
  • 8
  • 41
  • 69
0

Duplicate from the comments on the original post, which seemed to provide a suitable work-around.

I suspect the problem is that the Windows Widget lacks support for .json file types. As a work-around, I suggest that you set your JavaScript object to a variable inside of a .js file and use getScript to retrieve and execute that JavaScript.

After doing so, that variable should be accessible in the global namespace.

Adam Terlson
  • 12,610
  • 4
  • 42
  • 63
  • This too works in my browser but not as a gadget. I'm guessing that the problem is something to do with ajax. – grc Jun 17 '11 at 06:39