【问题标题】:sending data using POST method in sencha extjs rest webservice在sencha extjs rest webservice中使用POST方法发送数据
【发布时间】:2018-02-15 23:44:29
【问题描述】:

我想通过使用 post 方法向我的网络服务器发送数据来保存模型, 我编写了这些代码并注意到 sencha 正在使用 GET 方法发送数据。 如何使用 POST 方法发送数据? 我的型号代码:

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'
        },
    }
});

保存调用代码:

{
            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',
                });

            }
        }

【问题讨论】:

    标签: javascript user-interface extjs sencha-touch sencha-cmd


    【解决方案1】:

    如果您使用的是model,那么您只需使用actionMethods

    actionMethods 动作名称到 HTTP 请求方法的映射。在基本的 AjaxProxy 中,这些设置为 'GET' 用于“读取”操作,“POST”用于“创建”、“更新”和“销毁”操作。默认为:

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

    在这个FIDDLE中,我使用modelbutton创建了一个演示。我希望这将帮助/指导您实现您的要求。

    *注意 我只使用了local 网址。我没有任何实时网址。您可以在网络请求中看到数据是在 url 中发送的。

    代码片段

    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);
                            }
                        });
                    }
                }]
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      相关资源
      最近更新 更多