请求失败时,无法在JSONP代理回调中获取响应

Cannot get response in JSONP proxy callback when request failed

本文关键字:回调 代理 获取 响应 JSONP 失败 请求      更新时间:2023-09-26

我正试图将一些逻辑附加到所有存储中。我希望这个逻辑在通过应用商店定义的逻辑之前执行

基本上,我想要一块代码来查看来自服务器的响应,如果它得到了特别的东西,就做一些事情,如果没有,那么就通过调用this.callParent()来继续处理将要发生的事情。

这似乎是我想要的,但它很旧,不适用于4.2.1

http://www.learnsomethings.com/2011/09/01/adding-some-error-detection-to-all-your-extjs-stores-with-one-simple-block-of-code/

下面是我在4.2.1中所做的,它似乎在中起作用

Ext.define('App.overrides.Data.Store', {
    override: 'Ext.data.Store',
    load: function (options) {
        if (options != null) {
            options.callback = storeLoad;
        }
        this.callParent(arguments);
    }
});
storeLoad: function (records, operation, success) {
    // operation.response is undefined on failures...       
}

但正如上面的评论中所指出的,如果请求失败,则我无法获得operation.response,代码为400401500等。

如果我浏览ExtJS在加载时生成的确切URL,该URL应该返回我想要的字符串的错误,我会得到:

Ext.data.JsonP.callback4({"Message":"The string!"})

此输出由我的返回。NET Web API后端:

actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, PortalResources.THE_STRING);

如果我将HttpStatusCode更改为OK,那么我将能够在上面的storeLoad中获得响应,但如果状态<>200.

我认为是我的后端没有正确返回响应,但ExtJS和/或我对它的理解似乎是问题所在。我认为这就是我的存储/代理的定义方式,因为当服务器返回<>状态时200,我甚至没有在Chrome的调试器中看到响应内容。

这是我的一家商店:

Ext.define('Customer_Portal_UI.store.Contact', {
    extend: 'Ext.data.Store',
    model: 'Customer_Portal_UI.model.Contact',
    limitParam: false,
    pageParam: false,
    startParam: false
});
//.......
Ext.create('Customer_Portal_UI.store.Contact', {
    storeId: 'myContactStore',
    proxy: {
        type: 'jsonp',
        url: this.Urls.contactGetAllApiUrl,
        headers: { 'Content-type': 'application/json' },
        reader: { type: 'json', root: 'Contacts' }
    }
});

我尝试了上面提到的覆盖,但问题相同。失败时未定义响应。我在框架链中一直向上爬,似乎找不到合适的覆盖来完成这项工作。

有什么想法吗?

我切换到了普通的Ajax代理,现在我可以正确地检索失败请求的响应文本。

这就是我最终所做的:

Ext.Ajax.on('requestexception', function (conn, response, options) {
        //If an AJAX request receives this response text & status, log the user out.
        //regardless of the resource that was queried
        if (response.responseText == GlobalVars.loginError && response.status == 408) {
            //the following stops extra logic from executing (if you had configured a 'failure' option on your request in the controller)
            delete options.failure;
            delete options.callback;
            //show the user a message box with an OK button that when clicked logs the user out...
            Utils.maxIdleReached();
        }
        else {
            //do nothing...
            //let controller logic do it's thing then as individual store/requests have 'failure' options configured for each....
        }
});

这捕获异常(接收状态代码400401408404500等的请求),无论它们是通过存储加载还是普通Ext.Ajax.request完成的。