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").
我正在尝试使用jquery访问存储在JSON文件中的数据(与小工具位于同一文件夹中)。以下示例在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
}
3 个解决方案
#1
5
You should read the file like text and then convert it to json. This utility should help you:
您应该像文本一样读取文件,然后将其转换为json。该实用程序可以帮助您:
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);
#2
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.
Windows小工具安全沙箱的限制,将干扰ajax的工作方式。当你将一个url传递给ajax调用时,它会对该url发出一个HTTP请求,并且以(file:// localpath)的形式存在一个url确实存在的浏览器,但是有一个windows小工具的东西有点像不同,即从window.location派生的相对url不能用作此处不存在的window对象。
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
#3
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.
我怀疑问题是Windows Widget缺乏对.json文件类型的支持。作为解决方法,我建议您将JavaScript对象设置为.js文件中的变量,并使用getScript来检索和执行该JavaScript。
After doing so, that variable should be accessible in the global namespace.
执行此操作后,应该可以在全局命名空间中访问该变量。
#1
5
You should read the file like text and then convert it to json. This utility should help you:
您应该像文本一样读取文件,然后将其转换为json。该实用程序可以帮助您:
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);
#2
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.
Windows小工具安全沙箱的限制,将干扰ajax的工作方式。当你将一个url传递给ajax调用时,它会对该url发出一个HTTP请求,并且以(file:// localpath)的形式存在一个url确实存在的浏览器,但是有一个windows小工具的东西有点像不同,即从window.location派生的相对url不能用作此处不存在的window对象。
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
#3
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.
我怀疑问题是Windows Widget缺乏对.json文件类型的支持。作为解决方法,我建议您将JavaScript对象设置为.js文件中的变量,并使用getScript来检索和执行该JavaScript。
After doing so, that variable should be accessible in the global namespace.
执行此操作后,应该可以在全局命名空间中访问该变量。