【问题标题】:How to decode string to object EXTJS 6如何将字符串解码为对象 EXTJS 6
【发布时间】:2016-06-04 09:17:56
【问题描述】:

我正在使用 ExtJS 从数据库创建一个 formPanel dynamyc(从服务器到对象 extjs 的字符串结果)

Ext.Ajax.request({
     async : false,
     method : 'POST',
     url : '/Devt/GetFormItemCustomization',
     params : {
        TableName : 'tabeltest',
        col : 1
    },
    success : function (response) {
        var results = Ext.decode(response.responseText);
        console.log(results );
        var form = Ext.create('Ext.form.Panel', {
            width : 300,
            bodyPadding : 10,
            renderTo : 'formCustDiv',
            name : "test",
            items : [results]
        });
    }
})

来自服务器的响应:

{
   fieldLabel:'Employee Id',
   xtype:'textfield',
   allowBlank:false,
},
{
   fieldLabel:'Nick Name',
   xtype:'textfield',
   allowBlank: false,
}

但这只会从最后一个数据创建一个对象。 对象

{
   fieldLabel: "Nick Name", 
   xtype: "textfield", 
   allowBlank: false
 }

我希望服务器解码的响应变成两个对象:

对象 {fieldLabel: "Employee Id", xtype: "textfield", allowBlank: false}

对象 {fieldLabel: "Nick Name", xtype: "textfield", allowBlank: false}

有什么建议吗?

【问题讨论】:

    标签: javascript json extjs decode extjs6


    【解决方案1】:

    嗯,您的服务器应该始终返回有效的 JSON。以逗号分隔的两个对象不是有效的 JSON。 (如果您不相信我,您可以使用众多在线 JSON 验证器之一进行检查。)您的服务器可能会返回一个对象数组吗?像这样:

    [
        {fieldLabel: "Employee Id", xtype: "textfield", allowBlank: false},
        {fieldLabel: "Nick Name", xtype: "textfield", allowBlank: false}
    ]
    

    在这种情况下,您可以轻松地继续并通过删除 results 变量周围的数组来创建一个表单:

    var form = Ext.create('Ext.form.Panel', {
            width : 300,
            bodyPadding : 10,
            renderTo : 'formCustDiv',
            name : "test",
            items : results
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      相关资源
      最近更新 更多