在Extjs中显示错误消息的最佳方式是什么?

vcirk6k6  于 2022-11-05  发布在  其他
关注(0)|答案(2)|浏览(168)

我正在使用store.synch()方法来发布数据。并且从服务器端进行验证,目前我正在使用消息框来显示错误消息。现在我想用不同的方法来显示错误,但不是markInvalid(),因为为此我还必须更改每个js字段和api。所以有没有markInvalid()的替代方法?

omtl5h9j

omtl5h9j1#

Extjs数据存储提供了监听器,其中一个监听器是exception(适用于ExtJs〈3.x)
下面是代码。

listeners: { //Exception Handler for the Ajax Request
    exception: function(proxy, response, operation){
        var error = Ext.decode(response.responseText);
        Ext.MessageBox.show({
            title: 'REMOTE EXCEPTION',
            msg: error.message,
            icon: Ext.MessageBox.ERROR,
            buttons: Ext.Msg.OK
        });
    }
}

顺便说一下,我没有得到什么是markInvalid()

sz81bmfz

sz81bmfz2#

你好Naresh Tank,我的解决方案是监控所有 AJAX 请求。这样你就可以发送你想要的错误消息,不管这是来自商店还是表单。
我希望这对你有帮助。
在app.js上

init: function() {
    this.addAjaxErrorHandler(this);
},
addAjaxErrorHandler: function(object) {
    Ext.Ajax.on('requestexception', function(conn, response, options, e) {

        var statusCode = response.status,
            errorText = null,
            captionText = response.statusText;

        if (statusCode === 0 || statusCode === 401) {
            Ext.Ajax.abortAll();
        }
        if(response.statusText==="Authorization Required"){
                Ext.Ajax.abortAll();
        }
        // 404 - file or method not found - special case
        if (statusCode == 404) {
            Ext.MessageBox.alert('Error 404', 'URL ' + response.request.options.url + ' not found');
            return;
        }

        if (response.responseText !== undefined) {
            var r = Ext.decode(response.responseText, true);

            if (r !== null) {

                errorText = r.ErrorMessage;
            }

            if (errorText === null)
                errorText = response.responseText;
        }

        if (!captionText)
            captionText = 'Error ' + statusCode;

        Ext.MessageBox.alert(captionText, errorText);
    },
                object);
    Ext.Ajax.on('requestcomplete', function(conn, response, options, e) {

        var statusCode = response.status,
            errorText = null,
            captionText = response.statusText;

        if (response.responseText !== undefined) {
            var r = Ext.decode(response.responseText, true);

            if (r !== null && r.success === false) {
                try{
                    if(typeof r.data[0].idUsr !== 'undefined')
                        return;
                }catch(e){}
                errorText = r.msg;

                if (errorText === null)
                    errorText = response.responseText;
                if (!captionText)
                    captionText = 'Error ' + statusCode;

                Ext.MessageBox.alert(captionText, errorText);
            }
        }
    },
                object);

};

相关问题