简体   繁体   English

EXTJS 4:无法从json文件加载数据

[英]EXTJS 4: Can not load data from json file

I am trying to load data from json file in my application but no data loaded, I am stuck with it: 我正在尝试从应用程序中的json文件加载数据,但未加载任何数据,我一直坚持下去:

(model)Singers.js: (模型)Singers.js:

Ext.define('SGM.model.Singers', {
extend: 'Ext.data.Model',
fields: ['id', 'ten_that', 'nghe_danh', 'ngay_sinh', 'tieu_su', 'anh_dai_dien', 'luot_like'],
idProperty: 'id',
proxy: {
    type: 'ajax',
    url: 'data/singers.json',
    reader: {
        type: 'json',
        root: 'data',
        successProperty: 'success'
    }
}

}); });

(store)Singers.js (商店)Singers.js

Ext.define('SGM.store.Singers', {
extend: 'Ext.data.Store',
model: 'SGM.model.Singers',
/*data:[
    {   id: 1,
        ten_that: "Bui Anh Tuan",
        nghe_danh: "Bui Anh Tuan",
        ngay_sinh:"20/9/1992",
        tieu_su: "Tieu su cua Bui Anh Tuan",
        anh_dai_dien:"buianhtuan.jpg",
        luot_like:100}, 
    {
        id: 2, 
        ten_that: "Cao Mi Kim", 
        nghe_danh: "Cao Mi Kim", 
        ngay_sinh:"20/9/1992", 
        tieu_su: "Tieu su cua Cao Mi Kim", 
        anh_dai_dien:"caomikim.jpg", 
        luot_like:100}, 
    {
        id: 3,
        ten_that: "Pham Khanh Phuong", 
        nghe_danh: "Khanh Phuong", 
        ngay_sinh:"20/9/1992", 
        tieu_su: "Tieu su cua Khanh Phuong", 
        anh_dai_dien:"khanhphuong.jpg", 
        luot_like:100}, 
    {
        id: 4, 
        ten_that: "Ho Quang Hieu", 
        nghe_danh: "Ho Quang Hieu", 
        ngay_sinh:"20/9/1992", 
        tieu_su: "Tieu su cua Ho Quang Hieu", 
        anh_dai_dien:"hoquanhieu.jpg", 
        luot_like:100}
],*/

}); });

The inline data above work well but when I create a separated json file: singers.js (in folder 'data') with the following code: 上面的内联数据运行良好,但是当我创建一个单独的json文件时:singers.js(在“数据”文件夹中)具有以下代码:

{
"success": true,    
"results": [
    {   "id": 1,
        "ten_that": "Bui Anh Tuan",
        "nghe_danh": "Bui Anh Tuan",
        "ngay_sinh":"20/9/1992",
        "tieu_su": "Tieu su cua Bui Anh Tuan",
        "anh_dai_dien": "buianhtuan.jpg",
        "luot_like": 100}, 
    {
        "id": 2, 
        "ten_that": "Cao Mi Kim", 
        "nghe_danh": "Cao Mi Kim", 
        "ngay_sinh": "20/9/1992", 
        "tieu_su": "Tieu su cua Cao Mi Kim", 
        "anh_dai_dien": "caomikim.jpg", 
        "luot_like":100}, 
    {
        "id": 3,
        "ten_that": "Pham Khanh Phuong", 
        "nghe_danh": "Khanh Phuong", 
        "ngay_sinh": "20/9/1992", 
        "tieu_su": "Tieu su cua Khanh Phuong", 
        "anh_dai_dien": "khanhphuong.jpg", 
        "luot_like": 100}, 
    {
        "id": 4, 
        "ten_that": "Ho Quang Hieu", 
        "nghe_danh": "Ho Quang Hieu", 
        "ngay_sinh": "20/9/1992", 
        "tieu_su": "Tieu su cua Ho Quang Hieu", 
        "anh_dai_dien": "hoquanhieu.jpg", 
        "luot_like": 100}
]

} }

And this is my folder directory: 这是我的文件夹目录:

+app
++controller
++model
+++Singers.js
++store
+++Singers.js
+view
+data
++singers.json
+app.js

-index.html.js -index.html.js

Thank in advanced! 谢谢进阶!

The error lies in the root property of your proxy/reader configuration. 错误在于您的代理/阅读器配置的root属性。 Here, you specify the value "data" as the root object, in your JSON file however, the objects are stored in the "results" property. 在这里,您将值"data"指定为根对象,但是在JSON文件中,这些对象存储在"results"属性中。

Try the following model definition (mind the value of root ): 尝试以下模型定义(注意root的值):

Ext.define('SGM.model.Singers', {
  extend: 'Ext.data.Model',
  fields: ['id', 'ten_that', 'nghe_danh', 'ngay_sinh', 'tieu_su', 'anh_dai_dien', 'luot_like'],
  idProperty: 'id',
  proxy: {
    type: 'ajax',
    url: 'data/singers.json',
    reader: {
      type: 'json',
      root: 'results',
      successProperty: 'success'
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM