ExtJS Ajax POST vs Proxy POST

ExtJS Ajax POST vs Proxy POST

本文关键字:POST Proxy vs ExtJS Ajax      更新时间:2023-09-26

我正在尝试使用 ExtJS 4.1 创建一个网格面板。它使用 AJAX 代理从服务器获取数据:

var store = Ext.create('Ext.data.Store', {
    model: 'myModel',
    pageSize: pageSize,
    proxy: {
        type: 'ajax',
        url: "../search",
        actionMethods:  {
            create: "POST",
            read: "POST",
            update: "POST",
            destroy: "POST"
        },
        headers: {
            'Content-Type': 'application/json'
        },
        limitParam: false,
        startParam: false,
        pageParam: false,
        extraParams: JSON.stringify({
            rows: pageSize,
            role: "Admin",
            index: myIndex,
            question: searchPhrase
        }),
        reader: {
            type: 'json',
            root: 'results.results',
            totalProperty: 'numFound',
            model: 'myModel'
        }
    }
});
store.loadPage(1);

但它似乎不起作用。

我收到一条错误消息,指出无法读取 JSON。更重要的是,在Firebug中,发送的参数不是人类可读的。

当我尝试使用相同的参数进行 Ajax 调用时,一切似乎都正常:

Ext.Ajax.request({
    url:"../search",
    method: "POST",
    params: JSON.stringify({
        rows: pageSize,
        role: "Admin",
        index: myIndex,
        question: searchPhrase
    }),
    success: function(){
        console.log("ok");
    },
    failure: function(response, opts){
        console.log("failed");
    },
    headers: {
        'Content-Type': 'application/json'
    }
});

即使在 Firebug 中,请求中的每个参数看起来也很好。

使用代理时,框架有什么不同?

我对存储使用以下代理配置(ExtJS v6.5.2):

proxy: {
    url: 'api/search',
    paramsAsJson: true,
    actionMethods: {
        read: 'POST'
    },
    type: 'ajax',
    reader: {type: 'json'}
},

将参数作为 JSON 发送:

{"page":1,"start":0,"limit":25}

这似乎是另一个 ExtJS 问题。

我在这里找到了一个修复程序

http://www.sencha.com/forum/showthread.php?196194-Ajax-Store-Send-Params-as-JSON-Body