使用Sencha touch从服务器读取数据

时间:2011-10-27 14:10:06

标签: c# asp.net json sencha-touch

我在sencha touch中进行了测试,代码如下

test.js

Ext.setup({
   onReady: function() {        
        Ext.regModel('car', {
            fields: [                               
                {name: 'manufacture',type: 'string'},
                {name: 'model',   type: 'string'},
                {name: 'price',    type: 'decimal'}
            ]
        });
        var productsList = new Ext.DataView({
            store: new Ext.data.Store({
                model: 'car',
                proxy: {
                    type: 'ajax',
                    url : 'cars.json',
                    reader: {
                        type: 'json',
                        root: 'data'
                    }
                },
                autoLoad : true
            }),
            tpl: new Ext.XTemplate(
                '<tpl for=".">',
                    '<div class="item">',
                        '"{manufacture}"',
                        '"{model}"',
                        '"{price}"',
                    '</div>',
                '</tpl>'
            ),
            itemSelector: "div.item",
            fullscreen: true
        });
   }
});

json代码是(cars.json)

  

{ “数据”:[{ “制造”: “双龙”, “模型”: “爱腾”, “价格”:100000},{ “制造”: “SUZUKI”, “模型”: “奥拓”, “价格”:80000}], “结果”:3}

此示例有效,但我已从服务器读取数据,来自c#/ aspx的代码如下

protected void Page_Load(object sender, EventArgs e)
{
    List<car> oList = new List<car>();

    car c = new car();
    c.manufacture = "SSANYONG";
    c.model = "ACTYON";
    c.price = 100000;

    car d = new car();
    d.manufacture = "SUZUKI";
    d.model = "ALTO";
    d.price = 80000;

    oList.Add(c);
    oList.Add(d);
    string sJSON;
    System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    sJSON = oSerializer.Serialize(oList);
    sJSON=string.Concat("{\"data\":", sJSON);
    sJSON=string.Concat(sJSON,",\"results\":3}");

    Response.ContentType = "application/json";
    Response.Write(sJSON);
}

汽车类是

public class car
{
    public string manufacture { get; set; }
    public string model { get; set; }
    public double price { get; set; }
}

当我运行aspx时,在localhost中显示与cars.json相同的文本,我尝试使用不同的代码

将“application / json”更改为“application / x-json”,         “application / json”改为“text / javascript”,         “代理类型:ajax”到“scripttag”,         “代理类型:ajax”到“休息”

不能正常工作。

1 个答案:

答案 0 :(得分:0)

示例终于工作了!只需要在服务器上容纳APPLICATION aspx(不是localhost和主管部门课程),还要在服务器上托管sencha touch应用程序以便一切正常工作