【问题标题】:Handling 404 exceptions in Sencha touch Store with an ajax proxy使用 ajax 代理处理 Sencha touch Store 中的 404 异常
【发布时间】:2011-10-30 11:47:40
【问题描述】:

我试图通过处理各种可能发生的异常来使我的代码更加健壮。一个可能是 Json Web 请求上的 404 异常。当 Json 请求收到 404 异常时,似乎没有调用 store.load 的回调方法。

代码:

    Ext.regModel('Activiteit', {
    fields: [
        { name: 'id', type: 'int' },
        { name: 'ServerId', type: 'int', mapping: 'Id' },
        { name: 'Title', type: 'string' },
        { name: 'Description', type: 'string' },
    ],
});

Ext.regApplication({
    name: 'App',
    launch: function () {
        console.log('launch');

        var ajaxActiviteitStore = new Ext.data.Store({
            model: "Activiteit",
            storeId: 'ajaxActiviteitStore',
            proxy: {
                type: 'ajax',
                url: '/senchatest/Activiteit/Gett/',
                reader: {
                    type: 'json',
                    root: 'activiteiten'
                }
            }
        });

        ajaxActiviteitStore.load(function (records, operation, success) {
            //the operation object contains all of the details of the load operation
            console.log(success);
        });
    }
});

这会导致 sencha-touch-debug.js 的第 7212 行出现“未捕获的类型错误:无法读取未定义的属性 'length'”异常。我使用的是 sencha touch 1.1.0 版。

堆栈跟踪:

Uncaught TypeError: Cannot read property 'length' of undefined
Ext.data.Store.Ext.extend.loadRecords  sencha-touch-debug.js:7212
Ext.data.Store.Ext.extend.onProxyLoad  sencha-touch-debug.js:7024
(anonymous function)  sencha-touch-debug.js:8742
Ext.data.Connection.Ext.extend.onComplete  sencha-touch-debug.js:17566
Ext.data.Connection.Ext.extend.onStateChange  sencha-touch-debug.js:17513
(anonymous function)  sencha-touch-debug.js:3421

我在这里做错了什么?

我通过向代理添加一个侦听“异常”事件的侦听器找到了解决方法,但我更希望调用存储加载的回调函数。我做错了什么,还是这是默认行为?

谢谢,

砂光机

【问题讨论】:

    标签: exception-handling sencha-touch extjs


    【解决方案1】:

    如果服务器返回错误(404、500、...),我会遇到与 AjaxProxy (ST 1.1.0) 相同的异常(未捕获的类型错误:无法读取未定义的属性“长度”)。

    其实我觉得问题出在Ext.data.AjaxProxy.createRequestCallback方法上。 我用这样的脏代码解决了我的问题:

    var ajaxActiviteitStore = new Ext.data.Store({
        model: "Activiteit",
        storeId: 'ajaxActiviteitStore',
        proxy: {
            type: 'ajax',
            url: 'some nonexistent url',
            reader: {
                type: 'json',
                root: 'activiteiten'
            },
            listeners: {
                exception: function(store, response, op) {
                   console.log('Exception !');
    
                   // hack to avoid js exception :
                   // TypeError: 'undefined' is not an object (evaluating 'records.length')
                   // on line sencha-touch-debug-1-1-0.js:7212
                   op.records = [];
                }
            }
        }
    });
    

    希望这能有所帮助,我会在 sencha-touch 论坛上打开一个问题。

    【讨论】:

    • 我选择只使用 jQuery 的 XHR 包装器而不是 Sencha Ajax 代理并手动创建和添加/更新(本地)存储记录
    【解决方案2】:

    我认为您还有另一个问题,即不存在的 url。不过,试试这个:

    var storeException = 0; 
    
    this.ajaxActiviteitStore = new Ext.data.Store({
        model: "Activiteit",
        storeId: 'ajaxActiviteitStore',
        proxy: {
            type: 'ajax',
            url: 'some nonexistent url',
            reader: {
                type: 'json',
                root: 'activiteiten'
            },
        listeners: {
                exception: {
                fn: function(proxy, response, operation ) {                         
                            // you can parse `response.responseText` to make some decisions
                        storeException = 404;                           
                    }
            }
            }
        }
    });
    
    this.ajaxActiviteitStore.load({
        scope: this,
        callback: function (records, operation, success) {
            if (storeException==0) {
                // do something
            } else {
                alert('Service unaviable');
            }
        }
    });
    

    【讨论】:

    • 你好 Xupypr MV,tnx 快速回复。我的问题是甚至从未调用过回调。我已经编辑了原始帖子并包含了完整的源代码。
    • 嗯...我想这是因为我使用 ExtJS4 而不是 Touch。您可以在代码中添加完整的错误堆栈跟踪并突出显示错误字符串吗?
    • 当然,stacktrace 包含在原始帖子中!
    猜你喜欢
    • 2012-08-02
    • 2012-12-07
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多