簡體   English   中英

在sencha extjs rest webservice中使用POST方法發送數據

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

我想通過使用post方法將數據發送到我的Web服務器來保存模型,我編寫了這些代碼,並注意到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', }); } } 

如果使用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);
                        }
                    });
                }
            }]
        });
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM