javascript JSON 格式的 Ext.data.store POST 数据问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9548263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:02:00  来源:igfitidea点击:

Ext.data.store POST data in JSON format issue

javascriptjsonextjsextjs4http-post

提问by user1247132

I'm trying to make EXT JSON store to send data using JSON, however it does not seem to be working. Here is simple code:

我正在尝试使 EXT JSON 存储使用 JSON 发送数据,但是它似乎不起作用。这是简单的代码:

       var myStore = new Ext.data.Store({
    //model: 'User',
    proxy: {
        type: 'ajax',
        url: '/users.svc',
        reader: {
            type: 'json',
            root: 'users'
        },
        writer: {
            type: 'json',
            root: 'data'
        },
        actionMethods: {
            create: 'POST', read: 'POST', update: 'POST', destroy: 'POST'
        },
        extraParams: { test: 'test' }
    },
        listeners: {
            beforeload: function (store, operation, options) {
                //alert(operation.params);
            }
        },
    autoLoad: true
});

Since I defined JSON "writer", my expectation that parameterswould be send to server using JSON. However it's still doing regular POST with following body: test=test&page=1&start=0&limit=25

由于我定义了 JSON“编写器”,因此我期望使用 JSON 将参数发送到服务器。但是,它仍然使用以下正文进行常规 POST: test=test&page=1&start=0&limit=25

While my expectation is that POST should have the following body: {test:'test',page:1,start:0}

虽然我的期望是 POST 应该具有以下正文: {test:'test',page:1,start:0}

I would appreciate any help

我将不胜感激任何帮助

P.S. I'm using EXTJS 4.0.7

PS 我正在使用 EXTJS 4.0.7

回答by Song

proxy.read always use params, NOT jsonData, so store.load can't post json

proxy.read 总是使用参数,而不是 jsonData,所以 store.load 不能发布 json

http://ahlearns.wordpress.com/2012/08/16/ext-js-4-load-a-data-store-using-json-params/

http://ahlearns.wordpress.com/2012/08/16/ext-js-4-load-a-data-store-using-json-params/

Ext.define('Ext.ux.data.proxy.JsonAjaxProxy', {
extend:'Ext.data.proxy.Ajax',
alias:'proxy.jsonajax',

actionMethods : {
    create: "POST",
    read: "POST",
    update: "POST",
    destroy: "POST"
},

buildRequest:function (operation) {
    var request = this.callParent(arguments);

        // For documentation on jsonData see Ext.Ajax.request
        request.jsonData = request.params;
        request.params = {};

        return request;
},

/*
 * @override
 * Inherit docs. We don't apply any encoding here because
 * all of the direct requests go out as jsonData
 */
applyEncoding: function(value){
    return value;
}

});

Hope this helps!

希望这可以帮助!

回答by Varun Achar

Shift the proxydefinition to the model.

proxy定义移至model.

E.g.

例如

Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'email'],
proxy: {
    type: 'ajax',
    url: '/users.svc',
    reader: {
        type: 'json',
        root: 'users'
    },
    writer: {
        type: 'json',
        root: 'data'
    },
    actionMethods: {
        create: 'POST', read: 'POST', update: 'POST', destroy: 'POST'
    },
    extraParams: { test: 'test' }
}
});

Then configure the store like so:

然后像这样配置商店:

 var myStore = new Ext.data.Store({
   model: 'User'
   });

The store will use the proxy specified in the model. Hope this helps!

商店将使用模型中指定的代理。希望这可以帮助!