请求失败时,无法在JSONP代理回调中获取响应
Cannot get response in JSONP proxy callback when request failed
我正试图将一些逻辑附加到所有存储中。我希望这个逻辑在通过应用商店定义的逻辑之前执行
基本上,我想要一块代码来查看来自服务器的响应,如果它得到了特别的东西,就做一些事情,如果没有,那么就通过调用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
完成的。
- AngularJS:我可以跳过函数参数回调吗
- 要求未定义JS回调参数
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 自引用回调
- 测试Angular Service解决错误回调中的promise
- 如何将一个JavaScript函数回调为多个函数
- JavaScript回调函数
- 用于回调的javascript参数
- 将json回调数据转换为日期
- 承诺在非节点式回调上使用Bluebird
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 如何使用jqueryAJAX从页面中回调多个变量
- jquery Onclick函数带有导致双击的回调排序函数
- Spine javascript无法解除代理回调的绑定
- 请求失败时,无法在JSONP代理回调中获取响应
- IE FB.login回调未运行,XD代理窗口保持打开状态
- SignalR JS hub代理的多个实例,实现多次函数回调
- 用代理访问回调方法中的对话框