PDA

View Full Version : How catch error from non-AJAX request?



simonr25
27 Jul 2013, 7:02 PM
Hello,

The code below shows an actioncolumn, in a gridpanel, which when selected downloads
a file. If the download fails, a failure status is sent from the server
(response.setStatus(...)).
The problem is that the response is shown in the document (the whole browser) and
(as this is a single-page app) the user needs to restart the app.

Is there a way of catching the response returned so that an alert can be displayed
indicating failure and the user will return to the previous page and can continue?

columns : [{
...
header: 'Download',
xtype : 'actioncolumn',

handler: function(grid, rowIndex, colIndex) {
var g = Ext.getCmp('gridlist');
var rec = g.store.getAt(rowIndex);
var par= rec.get('id');
var hrf = "inline/download.action?id="+par;
window.location.assign(hrf);
}

},

Can one add an exception function?
I did try catching the response status in Ext.Ajax.on('requestexception'... which didn't work.


Thank you,
S

skirtle
27 Jul 2013, 11:02 PM
If you load the download in a hidden iframe you can listen for suitable events on the iframe.

simonr25
28 Jul 2013, 1:58 PM
Hello,
Thank you for reply. (Am not finding this easy to resolve).

Have created a hidden iframe as below:
Ext.define('App.view.DownloadPdf', {
extend:'Ext.Component',
alias:'widget.DownloadPdf',
id: 'downloadiframe-win',
autoEl: {
tag: 'iframe',
cls: 'x-hidden',
src: ''
}
});

On the server, have:
response.setStatus(610);
response.flushBuffer();

1) What event handler should I use to catch the response error status?

2) Following the line, Ext.getDom('downloadiframe-win').src = "download.action?id="+par;
the console is outputting the following which I don't understand (would you know why this is happening)?:
Uncaught TypeError: Cannot set property 'src' of null

Have placed Ext.requires App.view.DownloadPdf in viewport.js
Have created an instance of App.view.DownloadPdf.

skirtle
28 Jul 2013, 11:03 PM
1. What's a 610?

I usually listen for a load event on the iframe. Not sure which status codes cause that to fire, you might need to use 200. The key thing is it doesn't fire for a successful download.

2. Have you rendered the component? I'd just use cmp.el.dom to access it.

Some related threads.

http://www.sencha.com/forum/showthread.php?265406
http://www.sencha.com/forum/showthread.php?246673