我正在尝试向所有商店添加一些逻辑。我希望此逻辑在整个应用程序商店定义的逻辑之前执行
基本上,我想要一段代码来查看服务器的响应,如果它特别返回某些内容,则执行某些操作,如果没有,则通过调用 this.callParent 继续执行将要发生的任何操作()
我猜。
这似乎符合我的要求,但它太旧了,不适用于 4.2.1
现在这是我在 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...
}
但正如上面评论中所述,如果请求失败并显示 400,401,500 等代码,我无法获得 operation.response
。
如果我浏览 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' }
}
});
我尝试了提到的覆盖 in there ,但同样的问题。失败时的响应未定义。我在框架链中一路爬行,但似乎找不到合适的覆盖来执行此操作。
有什么想法吗?
最佳答案
我切换到普通 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....
}
});
这会捕获异常(收到状态代码 400、401、408、404、500 等的请求),无论这些异常是通过存储加载还是普通的 Ext.Ajax.request
完成的。
关于javascript - 请求失败时无法在 JSONP 代理回调中获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171506/