backbone.js无法从url中获取数据?

时间:2013-12-03 13:06:01

标签: javascript backbone.js

前几天我有这个工作,我不知道我做了什么不同但我无法获取要添加到我的收藏中的数据。从教程和文档中,这段代码应该正常工作吗?

var Player = Backbone.Model.extend({});

var PlayersCollection = Backbone.Collection.extend({
  url: "data/players.json",
  model: Player
});

var playersCollection = new PlayersCollection();

playersCollection.fetch({
    success: function(players) {
      alert('success')
    },
    error: function() {
      alert('fail')
    }
});

我得到了错误,我想我错过了一些非常容易的东西。也许这是我的JSON,这是看看它。

[
    {
       "name": "JELLY Bryant",
       "team": "Ballaz",
       "team_id": "1",
       "number": "24",
    },
    {
       "name": "Lebron James",
       "team": "Miami Heat",
       "team_id": "2",
       "number": "6"
    },
    {
       "name": "Dwayne Wade",
       "team": "Miami Heat",
       "team_id": "2",
       "number": "3"
    },
    {
       "name": "Michael Beasley",
       "team": "Miami Heat",
       "team_id": "2",
       "number": "30"
    },
    {
       "name": "Carmelo Anthony",
       "team": "New York Knicks",
       "team_id": "3",
       "number": "15" 
    },
    {
       "name": "Ron Artest",
       "team": "New York Knicks",
       "team_id": "3",
       "number": "5" 
    },
    {
       "name": "Karl Malone",
       "team": "Los Angeles Lakers",
       "team_id": "1",
       "number": "33"
    },
    {
       "name": "Damion Lillard",
       "team": "Portland Trailblazers",
       "team_id": "4",
       "number": "3" 
    },
    {
       "name": "Westly Matthews",
       "team": "Portland Trailblazers",
       "team_id": "4",
       "number": "55" 
    },
    {
       "name": "Wilt Chamberlin",
       "team": "Los Angeles Lakers",
       "team_id": "1",
       "number": "17" 
    }
]

在网络标签(chrome dev工具)中,它确实成功地获得了json。

Request URL:http://localhost/FRESH/data/players.json
Request Method:GET
Status Code:200 OK (from cache)

我必须在这里遗漏一些内容。我有一些大型代码从硬编码集合中获取数据,然后当我切换到url方法时它没有工作,所以我将它剥离到基本的基础,所以它显然是一些让我感到害羞的东西。

WOOOOOOW 我看到我在第一个json模型“JELLY Bryant”的末尾添加了一个额外的逗号,这解决了它,我不认为这是一个大问题,我只是注意到它现在

1 个答案:

答案 0 :(得分:1)

您的服务器发送无效的JSON:您在第一个对象中有一个悬空的逗号。检查http://json.org/有效JSON格式的构成,http://jsonlint.com/等在线工具可以快速检查。

尝试

[
    {
       "name": "JELLY Bryant",
       "team": "Ballaz",
       "team_id": "1",
       "number": "24"
    }
]