adjusting json format send by extjs form

旧巷老猫 提交于 2019-12-11 11:39:01

问题


I have a question.

I need to send a json format to my backend service. It requires something i haven't managed to create. What i am sending with a form is this:

{
"jobs": {
    "name": "dsvs",
    "jobType": "CUSTOM",
    "description": "sdvsdv",
    "tasks": "14,15,16"
}
}

but what i need to send is

{
"jobs": {
    "name": "dsvs",
    "jobType": "CUSTOM",
    "description": "sdvsdv",
    "tasks": [14,15,16]
}
} 

how can i do this?

This is my form handler:

handler: function () {
                    var form = this.up('form').getForm();
                    var formData = Ext.encode(form.getValues());
                    Ext.Ajax.request({

                        url: ND.url + 'dna/rjs/secure/service/rest/jobs.json',
                        method: 'POST',
                        waitTitle: 'Connecting',
                        waitMsg: 'Sending data...',
                        jsonData: {
                            jobs: form.getValues()
                        }


                            });


                    })

I hope anyone has an idea!


回答1:


You can't by "honest" ways.

You can, however, hack it into that one.

handler: function () {
                    var form = this.up('form').getForm();
                    var formData = Ext.encode(form.getValues());
                    formData.jobs.tasks = formData.jobs.tasks.split(',');
                    Ext.Ajax.request({

                        url: ND.url + 'dna/rjs/secure/service/rest/jobs.json',
                        method: 'POST',
                        waitTitle: 'Connecting',
                        waitMsg: 'Sending data...',
                        jsonData: {
                            jobs: formData
                        }
                   });
})

You don't use your formData in your example, despite having it, BTW.

That would result in:

{
"jobs": {
    "name": "dsvs",
    "jobType": "CUSTOM",
    "description": "sdvsdv",
    "tasks": ["14","15","16"]
}
} 

If that is still not suitable, than you can further hack it by calling parseInt on each task value.

EDIT:

Added clarification.



来源:https://stackoverflow.com/questions/7340009/adjusting-json-format-send-by-extjs-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!