在backbone.js中获取时Json中的语法错误

时间:2014-01-10 12:16:59

标签: javascript json backbone.js

我正在尝试从JSON获取数据,并尝试显示它。在获取json时,它会在fetch中引发错误。这段时间我被困在这一点上,从谷歌读完后,我很困惑。任何人都可以帮我解决错误是什么以及如何继续。

<!DOCTYPE html>
<html>
     <head>
  <title>Fortified Studio</title>
 </head>
 <body>test

  <div id="profiles"></div>

        <script id="profileTemplate" type="text/template">
            <div class="profile">
                <div class="info">
                    <div class="name">
                        <%= name %>
                    </div>

                </div>
            </div>
        </script>
  </body>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>   
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
    <script>
    $(function() {
        console.log("start of script");
    var Prof = Backbone.Model.extend({});
    var ProfList= Backbone.Collection.extend ({
    model: Prof,
    url : '/data/testjson2.json'

    }); 
    var ProfView = Backbone.View.extend ({

        initialize: function (){
            console.log("View initialize");
            this.render();
        },
        render : function (){
            console.log("in render");
            template: _.template($('#profileTemplate').html()),
            _.each (this.collection, function(Prof) {
                var profileTemplate = this.template(Prof.toJSON());
                $(this.el).append(profileTemplate);
            }, this);
            console.log (this);
            return this;
        }


    });


 var profs = new ProfList ();
 var profViews = new ProfView ();
  new ProfList().fetch({
      sucess : function(){
          console.log('json loaded');
      },
      error: function (){
            console.log("error retrieving model");
      }

  });

     profViews.render();
  }); 




    </script>

</html>

我的JSON是: -

[
    {
        "name": "Johny Johny",          
    },
    {
        "name": "Jack n Jill",
    }
]

并在控制台上输出: -

.......
Unknown property 'box-sizing'.  Declaration dropped.               myFile.html
"start of script"                                                 myFile.html:27
"View initialize"                                                 myFile.html:37
"in render"                                                       myFile.html:41
[object Object]                                                   myFile.html:47
"in render"                                                       myFile.html:41
[object Object]                                                   myFile.html:47
 GET /data/testjson2.json        [HTTP/1.1 200 OK 3ms]
syntax error                                                      testjson2.json:1
 "error retrieving model"                                          myFile.html:62

请帮帮我,如何继续。

1 个答案:

答案 0 :(得分:1)

删除每个值集末尾的逗号。

[
  {
    "name":"Johny Johny"
  },
  {
    "name":"Jack n Jill"
  }
]

使用以下方法测试您的JSON: http://jsonformatter.curiousconcept.com/