最近在学习ext。。遇到了与后台交互的问题了。。我自己做了很简单的例子可是不知道为什么。数据就是读不出来。。请大家帮帮看。。贴源码:Ext.onReady(function(){


var re = new Ext.data.Record.create([{
name : "id",
type : "int",
mapping : "id"
}, {
name : "name",
type : "string",
mapping : "name"
}, {
name : "sex",
type : "string",
mapping : "sex"
}, {
name : "eamli",
type : "string",
mapping : "eamli"
}
]);

var store = new Ext.data.Store({
url : "http://localhost:8080/ex/user",
reader : new Ext.data.JsonReader({
}, re)
}); var cm = new Ext.grid.ColumnModel([
{header:"编号",dataIndex:"id"},
{header:"姓名",dataIndex:"name"},
{header:"性别",dataIndex:"sex"},
{header:"eamli",dataIndex:"eamli"}
]);
store.load({});
var grid = new Ext.grid.GridPanel({
title:"个人信息管理",
store:store,
cm:cm
});

new Ext.Viewport({
layout:"fit",
items:[grid]
});
});后台会返回一个JSON的对象:{"user":{"eamli":"dsfsd","id":5,"name":"sds","sex":"dsa"}},后台能够正确返回的。这个我试验过了。。请大家帮忙看看有错误的呢?

解决方案 »

  1.   

    先用假数据确保grid能出来 Try:
    var store = new Ext.data.JsonStore({
            url : "http://localhost:8080/ex/user",
            autoLoad: true,//自动加载数据
             root:'user',
            fields: ['eamli', 'id', 'name','sex']
        });
       
      

  2.   


    Ext.onReady(function () {
                 var store = new Ext.data.JsonStore({
                     id: 'store',                 
                     url: 'data.htm',
                     root: 'user',
                     fields: ['id','name','sex','eamli']
                 });
             var cm = new Ext.grid.ColumnModel([
            { header: "编号", dataIndex: "id" },
            { header: "姓名", dataIndex: "name" },
            { header: "性别", dataIndex: "sex" },
            { header: "eamli", dataIndex: "eamli" }
            ]);
             store.load({});
             var grid = new Ext.grid.GridPanel({
                 title: "个人信息管理",
                 store: store,
                 cm: cm
             });         new Ext.Viewport({
                 layout: "fit",
                 items: [grid]
             });
         });返回的格式也有问题:
    要这样{"user":[{"eamli":"dsfsd","id":5,"name":"sds","sex":"dsa"}]}
      

  3.   

    返回两条数据:{"user":[{"eamli":"dsfsd","id":5,"name":"sds","sex":"dsa"},{"eamli":"dsfsd2","id":5,"name":"sds2","sex":"dsa2"}]}
      

  4.   

    返回的json是用工具生成返回的。。
      

  5.   

    你用FIREDEBUG看下控制台,如果能夠獲取到AJAX返回的JSON對象,查看下字符串,如果有jason,那麼應該是格式不符合吧。
      

  6.   

    后台你是要对一个list或数组进行json转换。.你那个 json就只是对一个类进行json输出嘛
      

  7.   

    var dsFields = [];
    for ( var i = 0; i < cm.columns.length; i++) {
    dsFields.push(cm.columns[i].dataIndex);
    }
    var dsReaderOptions = {
    root : "users", //数据集获取根目录
    fields : dsFields
    //数据集配置
    };json : {user:[{name:"a",age:23},{name:"b",age:21}]}