2013-07-24 86 views
1

我收到JSON按照控制檯,但我不斷收到這個錯誤的觀點,獲取JSON數據,但它沒有得到加載到Ember.js

Assertion failed: Your server returned a hash with the key 0 but you have no mapping for it 

,但此錯誤,

TypeError {} "Cannot call method 'toString' of undefined" 

以下是一個JSON響應,

[{"id":"1","last_name":"Solow","first_name":"Jeanne","suffix":null,"expiration":"2013-11-15","email":"[email protected]","street":"16 Ludden Dr.","city":"Austin","state":"TX","zip":"33347","phone":"964-665-8735","interests":"Great Depression,Spanish-American War,Westward movement,Civil Rights,Sports"}, {etc..} 

這裏是我的app.js,

App = Ember.Application.create({}); 

App.Store = DS.Store.extend({ 
    revision : 12, 
    adapter : DS.RESTAdapter.extend({ 
     url : 'http://ankur1.local/index.php/api/example/users/format/json', 
     dataType : 'jsonp', 
    }) 
}); 

App.IndexRoute = Ember.Route.extend({ 
    renderTemplate : function() { 
     this.render('myTemplate', { 
      controller : 'Index' 
     }); 
    }, 
    model : function() { 
     return App.myTemplate.find(); 
    } 
}); 

App.IndexController = Ember.Controller.extend({ 
    user : Ember.Object.create({ 
     name : "" 
    }), 
    userNameBinding : Ember.Binding.oneWay("this.user.name"), 
    clickButton : function(name) { 

     if ($("#name").val().trim().length === 0) { 
      alert("text box is empty"); 
     } else { 

     } 
    } 
}); 

App.myTemplate = DS.Model.extend({ 
    id : DS.attr('int'), 
    last_name : DS.attr('string'), 
    first_name : DS.attr('string'), 
    suffix : DS.attr('string'), 
    expiration : DS.attr('date') 
}); 

需要注意的一點我在後端使用了Phil Sturgeon的Codeigniter RestServer庫。我的代碼可能有什麼問題,或者可能是後端問題?

回答

1

Ember數據要求json響應採用某種格式。基本密鑰需要是模型的名稱。在你的情況下,沒有基本密鑰。

例如:你正在返回以下

[{"id":"1", 
    "last_name":"Solow", 
    "first_name":"Jeanne", 
    "suffix":null, 
    "expiration":"2013-11-15", 
    "email":"[email protected]", 
    "street":"16 Ludden Dr."}, {etc}] 

但燼需要這樣的事情:

{'users': [{"id":"1", 
    "last_name":"Solow", 
    "first_name":"Jeanne", 
    "suffix":null, 
    "expiration":"2013-11-15", 
    "email":"[email protected]", 
    "street":"16 Ludden Dr."}, {etc}]} 

要麼你需要改變來自服務器的JSON響應,或利用其它用於與服務器接口的庫。