【问题标题】:Parsing json stored in localstorage - Error NS_ERROR_DOM_BAD_URI解析存储在本地存储中的 json - 错误 NS_ERROR_DOM_BAD_URI
【发布时间】: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


【解决方案1】:

我不太确定你想在这里做什么。

var test = JSON.parse(list_answ) || {}; // This returns an object

$.getJSON( test ); // this expects a uri

【讨论】:

  • 可能是一个逻辑错误,但我编辑了我的问题:我仍然遇到同样的错误
  • 仍然 - “list_answ” 是一个 json 字符串,它不是一个 URI。你明白吗?
  • 我想我现在开始明白了。你的意思是我只能使用 uri 来使用 getJSON,而不是存储的对象或字符串。我还阅读了这个链接,它对此有所了解stackoverflow.com/questions/13853016/…
  • 是的。 “getJSON”将检索一些 JSON 数据,如果该 JSON 字符串成功,则返回对象表示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 2016-04-29
  • 2022-07-06
  • 2020-01-05
  • 1970-01-01
相关资源
最近更新 更多