View Full Version : How can I tell the difference between reasons for aborted AJAX operations?

Tim Ward
20 Nov 2014, 4:50 AM
An AJAX operation may be aborted

(1) Because it has timed out

(2) Because the use has navigated away from he page (in the iframe) that initiated the request.

In case (1) obviously we want to show an error message to the user, as something has gone wrong that they need to know about.

In case (2) obviously we don't want to show an error message to the user, as the aborted AJAX call is a direct consequence of the user's decision to move to a different page.

But how can I tell the difference (in Ext JS 4)? In both cases I seem to get a response object with status 0 and statusText being the empty string?

Ext JS 3 code seemed to manage by checking statusText for 'communication failure', but I'm not getting that any more.

Tim Ward
20 Nov 2014, 7:10 AM
Ah, that's probably not what's really happening, in fact I can't find any signs that our code is being called at all in the event of a timeout.
What do we have to do to be told when an AJAX call times out in Ext JS 4?

20 Nov 2014, 2:51 PM

In the requestexception listener you can examine the response to see if timedout is set to true.

See: https://fiddle.sencha.com/#fiddle/do1

Tim Ward
21 Nov 2014, 1:29 AM
That would be helpful, except that the requestexception handler does not appear to be called on timeout.

21 Nov 2014, 2:30 PM
No? Seemed to work for me using the following test setup:

test code:

name : 'Fiddle',

launch : function() {
requestexception: function (conn, response, options) {
if (response.timedout) {
Ext.Msg.alert('Server error', 'Communication with the server timed out');
if (Ext.Ajax.setTimeout) {
} else {
Ext.Ajax.timeout = 2000;

url: 'data1.json'

test response:

success: true

* In the Fiddle I've set the delay on the response to 3000ms and the timeout on the Ajax requests to 2000ms to trip the timeout.

23 Nov 2014, 3:55 AM
Maybe you should use the window onbeforeunload event so you can handle things related to that before the Ajax call gets aborted and eventually adjust the way the AJAX handlers react in that case ;)

23 Nov 2014, 5:09 AM
Posted a similar thread here, unfortunately, with almost no interest:http://www.sencha.com/forum/showthread.php?294763-Proposition-regarding-Ext.data.proxy.Ajax-events

Tim Ward
24 Nov 2014, 1:28 AM
I'm now thinking that there's something more fundamentally wrong with how we've migrated this code from Ext JS 3 to Ext JS 4. But when I've got that fixed I'll certainly be looking for the timeout property.

Tim Ward
24 Nov 2014, 3:47 AM
Right, I've found out why the callback wasn't being called, and checking "timedout" then appears to give me what I want. What hadn't been converted from Ext JS 3 properly was setting the event handler on the proxy for the store for the grid: store.getProxy().on( "exception", exceptionhandler );