简体   繁体   English

如何将字符串解码为对象EXTJS 6

[英]How to decode string to object EXTJS 6

I'm I'm using ExtJS to create a formPanel dynamyc from database (string result from server to object extjs) 我正在使用ExtJS从数据库创建formPanel动态库(从服务器到对象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]
        });
    }
})

this response from server: 来自服务器的此响应:

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

but this only create one object from last data. 但这只会根据最新数据创建一个对象。 Object 宾语

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

i want response from server decode become two object: 我希望服务器解码的响应成为两个对象:

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

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

Any suggests? 有什么建议吗?

Well, your server should always return valid JSON. 好吧,您的服务器应该始终返回有效的JSON。 Two objects, comma-separated, isn't valid JSON. 两个对象(以逗号分隔)不是有效的JSON。 (You can check with one of the many online JSON validators if you don't believe me.) Could your server possibly return an array of objects? (如果您不相信我,可以与许多在线JSON验证器之一进行核对。)您的服务器能否返回对象数组? Like this: 像这样:

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

In that case, you could easily go ahead and create a form from these by removing the array around the results variable: 在这种情况下,您可以通过删除results变量周围的数组来轻松地从中创建表单:

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

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

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