【问题标题】:Getting error message from Store Proxy从 Store Proxy 获取错误消息
【发布时间】:2017-06-08 22:42:24
【问题描述】:

我想收到来自商店代理调用的错误消息。

我的商店是这样的:

Ext.define("MyStore", {
    extend: "Ext.data.Store",
    model: 'mymodel',
    proxy: {
        type: 'customrest',
        paramsAsJson: true,
        url: myUrl,
        actionMethods: {
            read: 'POST'
        },
        reader: {
            type: 'json',
            record: 'result',
            root: 'results'
        }
    },

    listeners: {
        load: function (store, records, success) {
            if (!success) {

            }else {
                //OK
        }
        }
    }
});

似乎只给我成功是假的,手头没有其他信息。我知道还会返回一条错误消息,因为返回的数据如下所示:

{成功:假,消息:“等等等等”}

有什么地方可以让我处理“消息”吗?

更新:

我的商店是这样称呼的:

this.psstore = Ext.getStore("MyStore"); 

this.psstore.load({
    params: postBody,
    callback: function (records, operation, success) {

        if (success) {

【问题讨论】:

  • 提供返回结果的代码(代理调用的代码或 API。)您需要处理该 API 的响应。 public String storeLoader(/*params*/){ if(successful){ "{success:true}" }else{ "{success:false,message: "出了点问题"}" } }

标签: extjs extjs4


【解决方案1】:

查看读者的messageProperty

http://docs.sencha.com/extjs/4.0.7/#!/api/Ext.data.reader.Json-cfg-messageProperty

所以在你的配置中你应该有:

    reader: {
        type: 'json',
        record: 'result',
        root: 'results',
        messageProperty: 'message'
    }

然后您应该能够通过加载回调中的 operation.exception 属性 (IIRC) 获取消息。它也是在 onLoad 事件 (http://docs.sencha.com/extjs/4.0.7/#!/api/Ext.data.Store-event-load) 中传递的参数,因此您应该也可以在那里访问它。我们在带有 XML 阅读器的 Ext 4 上使用它,并且在升级到 6 时使用它是安全的。

【讨论】:

    【解决方案2】:

    是的,有,但不,你不应该使用它。

    在您的情况下,在 ExtJS 6 之前的 ExtJS 中,您可以轻松地从 getStore().getProxy().getReader().rawData 读取整个原始响应,除非您在阅读器定义中设置了 keepRawData:false。这带有一个很大的但是

    在 6.0 中,如果您不在阅读器上使用 keepRawData,那么原始响应数据会很早就被丢弃。原因,引用自文档:请注意,从 Ext JS 6.0 开始,默认行为已更改为不保留原始数据,因为内存泄漏的可能性很高。

    因此,我在我的阅读器上为 ExtJS 6.0.1 添加了一个覆盖:

    Ext.define("MyApp.override.JsonReader", {
        override:"Ext.data.reader.Json",
        /**
         * Make additional processing available on the raw response.
         */
        processRawResponse:null,
        getResponseData:function(response) {
            if(this.processRawResponse) this.processRawResponse(response);
            me.callParent(arguments);
        }
    });
    

    所以现在我可以根据需要在每个阅读器上添加一个 processRawResponse 函数,比如这个:

    proxy: {
        type: 'ajax',
        groupParam: false,
        startParam:false,
        limitParam:false,
        pageParam:false,
        sortParam:false,
        url: '../api/AdminPanel/ACLRules',
        headers: {
            Accept: 'application/json'
        },
        reader: {
            type: 'json',
            rootProperty: 'data',
            processRawResponse:function(response) {
                var responseText = Ext.decode(response.responseText, true);
                if(responseText && responseText.message) {
                    Ext.Msg.alert('ERROR',responseText.message);
                }
    

    你可以试试这是否也适用于 ExtJS 4; from the source code, it seems so。否则,只需要进行微小的更改。

    如果您的错误消息每次都出现在同一个属性中并且每次都需要相同的处理,那么您也可以直接从覆盖全局处理它们,例如如果你得到一个调试信息数组:

    getResponseData:function(response) {
        if(this.processRawResponse) this.processRawResponse(response);
        var debugList = Ext.getCmp("debugList"),
            shorten = function(tex) {
                return tex.substring(0,500).replace(/(\r\n|\r|\n)/g,"<br>");
            },
            returned = "";
        try {
            returned = response.responseText;
            var decodedData = Ext.decode(returned);
            if(decodedData.Debug) this.rawDebugData = decodedData.Debug;
            return decodedData;
        } catch (ex) {
            var caption = "Decoding error",
                message = "The server has returned malformed JSON.",
                box = Ext.create('Ext.window.MessageBox',{});
            try {
                var jsonStart = Math.min(returned.indexOf('['), returned.indexOf('{'));
                if(jsonStart>0) {
                    message = (message + "\n" + returned.substring(0,jsonStart));
                    returned = returned.substring(jsonStart);
                }
                else {
                    message = (message + "\n" + returned);
                }
            } catch (e) {
            }
            if(!debugList) box.alert(caption,shorten(message));
            if(debugList) debugList.setValue(debugList.getValue()+caption+': '+message+'\n');
            return Ext.decode(returned, true) || {};
        }
    },
    

    【讨论】:

    • 哇,谢谢。我不知道它是如此复杂。我正在考虑只是切换到一个简单的 Ajax 调用而不是使用代理。代理似乎比它们的价值更令人头疼。
    • 基本上,使用代理有什么好处?在使用类似小粒度 CRUD 的操作时是否有优势?但是对于表中的简单数据转储.. 那么 AJAX 调用就足够了,而且更合适吧?
    • IMO,读者的简单覆盖并不复杂。我在底部的复杂覆盖是因为我正在尝试处理包含非 JSON 部分的响应。不要问那里到底发生了什么,我已经忘记了 ;-) 如果您需要具有所有 CRUD 操作的商店,则可以使用代理。另一方面,简单的 Ajax 调用的最大优势在于,如果需要,您可以捆绑数据以一次填充多个存储。这将减少对服务器的调用次数。
    • 你不需要做所有这些。当成功为假时,代理会触发exception 事件,您可以从操作中获取错误。这是一个小提琴:fiddle.sencha.com/#fiddle/1qbj
    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2020-01-19
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多