jQuery getJSON奇怪的解析错误

时间:2016-03-10 14:45:23

标签: javascript jquery html json

我有这个简单的JSON文件,其中包含文件夹和图像文件的路径:

{ "foldery" : [
    {
      "foName": "website/img/post1",
      "files" : [
        "website/img/post1/a.jpeg",
        "website/img/post1/b.jpg",
        "website/img/post1/c.jpeg",
        "website/img/post1/d.jpg",
        "website/img/post1/e.jpeg",
        "website/img/post1/f.jpg",
        "website/img/post1/g.jpg",
        "website/img/post1/h.jpg"
      ]
    },
    {
      "foName": "website/img/postdrugi",
      "files" : [
        "website/img/postdrugi/20150530_111106.jpg",
        "website/img/postdrugi/20150530_113842.jpg",
        "website/img/postdrugi/20150530_131251.jpg",
        "website/img/postdrugi/20151022_165146.jpg"
      ]
    }
  ]
}

点击元素时会触发一个小的getJSON函数:

function getImages(dir){

  function select(o) {

    var length = o.foldery.length;
    //getting the length, and firing a for for all of it's elements
    for(var i = 0; i < length; i++){
        if (o.foldery[i].foName == dir) {
          // if the foName is the same as the functions argument, place 'files' of it into array
            return o.foldery[i].files;
        }
    }
}

 $.getJSON("nameindex.json", function(o) {
   console.log("getJSON success");
   var result = select(o);
   var $thmb = $(".thmb .sec");
   var thumbLength = 0;
   $thmb.children().remove();
   for(var i = 0; i < result.length; i++){
           $thmb.append('<div class="img"><img src="'+result[i]+'" alt=""/></div>');
   }

    }).fail( function(d, textStatus, error) {
        console.error("getJSON failed, status: " + textStatus + ", error: " + error);
    });
}

现在,一天前,我的笔记本电脑上的功能运行良好。今天我将所有文件移动到相同的设置WAMP在不同的PC上,在chrome上运行代码(与以前一样工作)....并且没有任何反应。 '失败'功能让我回答:

  

getJSON失败,status:parsererror,error:SyntaxError:Unexpected   令牌]

这很奇怪,因为JSON文件(如果我的linter不对我撒谎)写得很好 在页面上,我目睹了错误jQuery被放置在<body>标签的开头,但我在其他地方遇到了同样的问题。 网页上的所有其他jQuery元素都可以正常工作。

是否有其他人目睹了这个问题并且可能知道解决方法? 另外,提供一个关于为什么会发生这种错误的小解释会很好。

编辑:好的,现在可行了。它开始独立工作 仍然没有删除或发布这个问题的答案,因为我真的很好奇以前出了什么问题。

小更新:即使在同一台机器上,代码似乎也非常不稳定。例如。有时它适用于file1.json,即使file2是file1的直接副本,也不适用于file2.json。

0 个答案:

没有答案
相关问题