简体   繁体   English

从 windows 小工具中的 JSON 文件检索数据时出现问题

[英]Problem retrieving data from JSON file in windows gadget

I am trying to access data stored in a JSON file (in the same folder as the gadget) using jquery.我正在尝试使用 jquery 访问存储在 JSON 文件(与小工具相同的文件夹中)中的数据。 The following example works fine in both firefox and internet explorer (shows "success"), but as a gadget it doesn't work (shows "fail").以下示例在 firefox 和 Internet Explorer 中都可以正常工作(显示“成功”),但作为小工具它不起作用(显示“失败”)。

$('#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
}

You should read the file like text and then convert it to json.您应该像文本一样阅读文件,然后将其转换为 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);

The windows gadgets security sandbox restrictions, will be interfering with the way ajax works. windows 小工具安全沙箱限制,将干扰 ajax 的工作方式。 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, ie the relative url which is derived from window.location cannot be used as the window object does not exist here. 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不同,即从 window.location 派生的相对 url 不能用作 window ZA8CFDE63331BD59EB2AC96F8 此处不存在。

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.这里最简单的方法是简单地将 json 放在 JS 文件中并使用脚本标签引用它,因为 HTML DOM 的那部分由处理渲染/加载内容的 sidebar.exe 代码处理。

Thanks谢谢

Neeraj尼拉吉

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.我怀疑问题是 Windows 小部件缺乏对.json 文件类型的支持。 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.作为一种解决方法,我建议您将 JavaScript object 设置为 a.js 文件中的一个变量,然后使用getScript检索并执行该 JavaScript。

After doing so, that variable should be accessible in the global namespace.这样做之后,该变量应该可以在全局命名空间中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM