PDA

View Full Version : How to get content when iframe loaded on IE



earist
20 Jun 2014, 3:59 AM
Hi all,

I am using iframe to download file from server.
If download fail, server will return error message as json.

Example error message:


{
"success": "fail",
"message": "The url is invalid"
}


Follows are sample code:



MY.download = function(config) {
var form, iframe, removeNode = MY.download.removeNode,frameId = 'myFrame',

iframe = Ext.core.DomHelper.append(document.body, {
id: frameId,
name: frameId,
style: 'display:none',
tag: 'IFRAME'
}),

inputs = paramsToInputs(config.params);


if (iframe.attachEvent)
iframe.attachEvent('onload', MY.download.onload);
else if ( iframe.addEventListener )
iframe.addEventListener('load', MY.download.onload, false);
else
iframe.onload = MY.download.onload;


form = Ext.DomHelper.append(document.body, {
action: config.url,
cn: inputs,
method: config.method || 'GET',
tag: 'form',
target: frameId
});

form.submit();

removeNode(form);

Ext.defer(removeNode, 1000 * 60 * 10, null, [iframe]);

function paramsToInputs(params) {
var inputs = [];

for (var key in params) {
var values = [].concat(params[key]);

Ext.each(values, function(value) {
inputs.push(createInput(key, value));
});
}
return inputs;
}

function createInput(key, value) {
return {
name: Ext.htmlEncode(key),
tag: 'input',
type: 'hidden',
value: Ext.htmlEncode(value)
};
}
};

MY.download.onload = function() {

var text = this.contentDocument.body.innerText;
if (!text)
text = this.contentDocument.body.textContent;

// text is always empty in IE
try
{
var json = Ext.decode(text);
alert(json.message);
}
catch(err)
{
alert('error');
}
};

MY.download.removeNode = function(node) {
node.onload = null;
node.parentNode.removeChild(node);
};




...
MY.download({
url: myURL,
method : 'GET'
});
...


In MY.download.onload, Firefox and Chrome work fine.
However, IE11 cannot get text, it always empty.

How can I fix this problem?

Thanks in advance
earist

earist
25 Jun 2014, 12:49 AM
Hi all,

I found that IE does not know what to do with application/json contents.
So. I changed the MediaType to text/plain or text/html instead and it work fine.
:)