【发布时间】:2015-09-29 22:58:47
【问题描述】:
我已经阅读了很多帖子,但都没有成功。我正在开发一个简单的 set/get localstorage 的 json 字符串化,如下面的link。
不幸的是,我总是得到这个错误:
请求失败:错误,NS_ERROR_DOM_BAD_URI:访问受限 URI 拒绝
这是我正在使用的一个片段(仅用于我的测试):
var id_expert = "1";
var testjson = "js/testjson.js";
$.getJSON(testjson)
.done(function( data ) {
//////// STORE ///////////
window.localStorage.setItem("id_expert_selected", id_expert);
var list = 'questions_'+id_expert;
window.localStorage.setItem(list,JSON.stringify(data));
////////// RETRIEVE //////////////
var id_exp = window.localStorage.getItem('id_expert_selected');
var list_answ = window.localStorage.getItem('questions_'+id_expert);
var test = JSON.parse(list_answ) || {};
$.getJSON( test )
.done(function( data ) { <---------- I'm not able to got it.
... bla bla bla...
在片段中,我存储/检索 json 字符串,但没有解析成功。 我确定 json 很好,因为当我在没有本地存储的情况下使用它时,我得到了解析。
有什么线索吗?
编辑:
即使使用它,我也得到了相同的结果:
$.getJSON( list_answ )
.done(function( data ) { <---------- I'm not able to got it.
... bla bla bla...
我还尝试更改 Firefox 配置中的标志(一些安全内容),但错误仍然存在。 在真正的Android设备上,在调试中,我读到了另一种错误描述:
file:///storage/emulated/0/Android/data/com.intel.html5tools.apppreview/cac…/previews/MyApp/1/%7B%22username%22:%22myusername%22,%22pwd% 22:%22mypwd%22%7D 净::ERR_FILE_NOT_FOUND
感谢MegaAppBear我解决了:
//// 存储 JSON /////
window.localStorage.setItem("id_expert_selected", id_expert);
var list = 'questions_'+id_expert;
window.localStorage.setItem(list,JSON.stringify(data));
//// 检索 JSON /////
var id_expert = window.localStorage.getItem('id_expert_selected');
var list_data = window.localStorage.getItem('questions_'+id_expert),
def = $.Deferred();
if (!list_data) {
def = $.getJSON('js/test.json', function(data) {
/// save data using localStorage.setItem
});
}else{
list_data = JSON.parse(list_data);
def.resolve();
}
def.done(function() {
var expert_name = document.getElementById('expert_name');
expert_name.textContent = list_data.NameExpert;
..... and so on .......
【问题讨论】:
-
好吧,似乎 list_answ 是一个 json 字符串,而您的测试变量是您发布的代码中的一个 json,所以当您尝试像 $.getJSON(test) 一样发出请求时,您传递的参数是一个对象,而不是作为字符串的 url
-
我编辑了我的问题。我仍然收到错误
标签: javascript json html cordova local-storage