PDA

View Full Version : Possible to cancel Ajax failure?



dr-dan
17 Oct 2012, 5:26 AM
I have introduced a watcher on all Ajax requests in my app - for when an authentication ticket expires, and a subsequent ajax request fails - I return a 403 and a bit of JSON with a standard error message.

When this occurs I fire an event from this catch all and the login form is shown. However I would like to cancel any further processing in the pipeline, because the original failure callback still fires:


watchAjaxAuthentication: function () {
var me = this;
Ext.Ajax.on('requestexception', function (conn, response, options, eOpts) {
if (response.status == 403) {
var responseObject = Ext.decode(response.responseText);
if (responseObject.authenticationError && responseObject.authenticationError == 'NotAuthenticated') {
me.fireEvent('sessionExpired');
Ext.Ajax.abort(); // not what I'm looking for, need something like cancelBubble() or setFailureHandled(true)
}
}
});
},



m.load(id, {
scope: this,
success: function (pegfile) {
// do stuff
},
failure: function () {
alert('oops, error loading record'); // dont do this if 403
}
});


Is there a way to cancel that?

dr-dan
18 Oct 2012, 12:53 AM
One solution: override `Ext.Ajax.onComplete` (from `app.launch`):


watchAjaxAuthentication: function () {
var app = this;
Ext.override(Ext.Ajax, {
onComplete: function (request) {
var me = this,
options = request.options,
result,
status,
success,
response;


try {
status = request.xhr.status;
} catch (e) {
// in some browsers we can't access the status if the readyState is not 4, so the request has failed
status = -1;
}
if (status == 403) {
response = me.createResponse(request)
var responseObject = Ext.decode(response.responseText);
if (responseObject.authenticationError && responseObject.authenticationError == 'NotAuthenticated') {
app.fireEvent('sessionExpired');
return;
}
}
this.callParent([request])
}
});
},