简体   繁体   English

在sencha extjs rest webservice中使用POST方法发送数据

[英]sending data using POST method in sencha extjs rest webservice

I wanna save a model by sending data using post method to my web server, I wrote these codes and noticed that sencha is sending data using the GET method. 我想通过使用post方法将数据发送到我的Web服务器来保存模型,我编写了这些代码,并注意到sencha使用GET方法发送数据。 how can I send the data using POST method? 如何使用POST方法发送数据? my model code: 我的模型代码:

 Ext.define('MyApp.model.emp.Roles', { extend: 'MyApp.model.Base', fields: [ { type: 'int', name: 'id' }, { type: 'string', name: 'title' }, ], proxy: { type: 'ajax', url : 'http://myweb.test/json/fa/myweb/managerole.jsp', idParam: 'id', extraParams: { 'action':'btnSave_Click' }, method:'POST', actionMethods: { create : 'POST', read : 'POST', update : 'POST', destroy: 'POST' }, } }); 

save calling code: 保存调用代码:

 { xtype: 'button', text: 'ذخیره', handler:function() { var user = Ext.create('MyApp.model.emp.Roles', {title: 'A Role From Sencha'}); user.set('title','hi'); user.set('id',-1); user.save({ params: user.getData(), callback: function (records, operation) { Ext.Msg.alert('User Save', operation.getResponse().responseText); }, methodName:'POST', method:'POST', }); } } 

If you are using model then you need to use only actionMethods . 如果使用model ,则只需要使用actionMethods

actionMethods Mapping of action name to HTTP request method. actionMethods操作名称到HTTP请求方法的映射。 In the basic AjaxProxy these are set to 'GET' for 'read' actions and 'POST' for 'create', 'update' and 'destroy' actions. 在基本的AjaxProxy中,对于“读取”操作,这些设置为“ GET”;对于“创建”,“更新”和“破坏”操作,这些设置为“ POST”。 Defaults to: 默认为:

{
    create: 'POST',
    read: 'GET',
    update: 'POST',
    destroy: 'POST'
} 

In this FIDDLE , I have created a demo using model and button . 在此FIDDLE中 ,我使用modelbutton创建了一个演示。 I hope this will help/guide your to achieve your requirement. 我希望这会帮助/指导您达到要求。

*NOTE I have used only local url. *注意:我只使用了local网址。 I don't have any live url. 我没有任何实时网址。 You can see in network request data is sending in url. 您可以在网络请求中看到数据正在以url发送。

CODE SNIPPET 代码片段

Ext.application({
    name: 'Fiddle',

    launch: function () {
        Ext.define('MyModel', {
            extend: 'Ext.data.Model',
            fields: ['version', 'code', 'framework', 'frameworkVersion', 'fiddleid', 'inspector', 'session'],
            proxy: {
                type: 'ajax',
                url: 'local', //I am providing local url in your case you can provide your rest webservice url
                useDefaultXhrHeader: false,
                actionMethods: {
                    create: 'POST', //When you want to save/create new record
                    read: 'GET', //When you want to get data from server side
                    update: 'PUT', //When you want to update the record
                    destroy: 'DELETE' //When you want to delete the record
                },
                paramAsJson: true // if You  want to encode the data from clint side then it should be true otherwise false
            }
        });

        Ext.create({
            fullscreen: true,
            renderTo: Ext.getBody(),
            xtype: 'panel',
            title: 'sending data using POST method in sencha extjs rest webservice',
            padding: 10,
            items: [{
                xtype: 'button',
                text: 'Send Data',
                margin: 15,
                style: {
                    background: '#ccc'
                },
                height: 50,
                width: '100%',
                handler: function () {
                    var MyModel = Ext.create('MyModel', {
                        version: "2",
                        code: '',
                        framework: "291",
                        frameworkVersion: "",
                        fiddleid: "",
                        inspector: "",
                        session: ''
                    });
                    MyModel.save({
                        success: function (records, operation) {
                            //When data will save on sever side then response will come in success
                            Ext.Msg.alert('User Save', 'Data saved');
                        },
                        failure: function (records, operation) {
                            //If some error occure on server side the reponse will come in failure function
                            Ext.Msg.alert(`Error ${operation.error.status}`, operation.error.statusText);
                        }
                    });
                }
            }]
        });
    }
});

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

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