简体   繁体   English

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

[英]Cannot get response in JSONP proxy callback when request failed

I'm trying to append some logic to all the stores. 我正在尝试向所有商店添加一些逻辑。 I want this logic to execute before the one that is defined all through the app's stores 我希望此逻辑在通过应用商店定义的所有逻辑之前执行

Basically, I want one chunk of code to look at the response from the server, if it gets something back in particular, do something, if not, then proceed with whatever was going to happen by calling this.callParent() I guess. 基本上,我想让一小段代码查看服务器的响应,如果它特别返回了某些内容,请执行一些操作(如果没有获得响应),然后通过调用this.callParent()来处理将要发生的this.callParent()

This seemed to do what I want but it's old and not working for 4.2.1 这似乎可以满足我的要求,但是它已经过时了,无法在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/ http://www.learnsomethings.com/2011/09/01/adding-some-error-detection-to-all-your-extjs-stores-with-one-simple-block-of-code/

Now here's what I do in 4.2.1 and it seems to be working 现在这是我在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...       
}

But as noted in the comment above, I cannot get an operation.response if the request failed with 400,401,500, etc. code. 但正如评论指出的上述,我不能让一个operation.response如果请求与400401500,等代码失败。

If I browse the exact URL ExtJS generates on load that is supposed to return an error with the string I want, I get this: 如果我浏览的是ExtJS加载时生成的确切URL,该URL应该返回我想要的字符串错误,我会得到以下信息:

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

This output is returned by my .NET Web API backend: 此输出由我的.NET Web API后端返回:

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

If I change the HttpStatusCode to OK , I will then be able to get the response in storeLoad above, but not if the status <> 200. 如果我改变HttpStatusCodeOK ,我会那么能够得到的响应storeLoad以上,但如果状态<> 200。

I thought it was my backend not returning the response properly, but ExtJS and/or my comprehension of it seems to be the problem. 我以为这是我的后端没有正确返回响应,但是ExtJS和/或我对此的理解似乎是问题所在。 I think it's how my store/proxy is defined since when the server return sa status of <> 200, I don't even see the response content in Chrome's debugger. 我认为这是我的存储/代理的定义方式,因为当服务器返回状态<> 200时,我什至没有在Chrome的调试器中看到响应内容。

Here's one of my stores: 这是我的一家商店:

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' }
    }
});

I tried the override mentioned in there , but same problem. 我尝试了那里提到的替代,但是同样的问题。 Response is undefined on failure. 对于失败,响应未定义。 I crawled all the way up in the framework chain and I can't seem to find a proper override to do this. 我一直在框架链中向上爬行,但似乎找不到合适的替代方法来做到这一点。

Any ideas ? 有任何想法吗 ?

I switched to a vanilla Ajax proxy, now I can retrieve the response text correctly on failed requests. 我切换到普通的Ajax代理,现在我可以在失败的请求中正确检索响应文本。

This is what I ended up doing: 这就是我最终要做的事情:

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....
        }
});

This catches exceptions (requests that received status codes of 400, 401, 408, 404, 500, etc.) whether they were done through a store load or plain Ext.Ajax.request . 这捕获了异常(接收到状态码为400、401、408、404、500等的请求),无论它们是通过存储加载还是通过简单的Ext.Ajax.request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM