【问题标题】:How to load json data from ajax in ExtJs如何在 ExtJs 中从 ajax 加载 json 数据
【发布时间】:2013-10-07 09:13:43
【问题描述】:

我尝试开始使用 ExtJs,但我有一个非常基本的问题。
我有模特

Ext.define('Mb.model.User', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'name', type: 'string' },
        ...
    ],
    proxy: {
        type: 'ajax',
        url : 'server/gui/getUser.php'
    }
});

getUser.php 返回一个 json 字符串(它是登录用户,而不是用户表中的用户):

{"id":"1","name": ... }

我尝试了以下方法来加载数据,但出现错误Uncaught TypeError: Object [object Object] has no method 'load'

Ext.define('Mb.Application', {
    ...
    launch: function() {
        ....
        user = Ext.create('Mb.model.User');
        user.load();
    }
});

加载该数据的正确方法是什么?


另一个问题:使用Modelhere 有什么好处?

我不能这样做吗?

Ext.Ajax.request({
    url: 'server/gui/getUser.php',
    success: function(response){
    var user = Ext.JSON.decode(response.responseText);
    }
});  

【问题讨论】:

    标签: ajax json extjs model extjs4.2


    【解决方案1】:

    在这种情况下 load 是一个静态方法。您可以通过传递 id 从服务器加载模型。

    Mb.model.User.load(id, {
        success: function(rec) {
            console.log('loaded', rec.getData());
        }
    });
    

    使用模型的优势在于抽象层,+ 使用模型获得的额外功能。

    【讨论】:

      【解决方案2】:

      你可以做,但需要做些小改动

      myRequest = Ext.Ajax.request({
        url: 'getdata.php',
        method: 'GET',
        callback: function(response) {
          console.log(response.responseText);
        }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 2012-06-10
        • 1970-01-01
        • 1970-01-01
        • 2013-11-03
        • 1970-01-01
        相关资源
        最近更新 更多