javascript - How to display error (text/html) response to an AJAX/getJSON request? -
my situation is, i'm developing little web app server provides dynamic json responses. server built on cherrypy. sometimes, there bug in code creating json data, throws, , cherrypy catches , serves 500-error full html page detailing exception. (that is, response has everything: <!doctype..><html><head>...</head><body>...</body></html>
) because request ajax, doesn't displayed.
i can intercept error enough, , @ in dev tools; i'd (to ease debugging) open new page (as if user had followed link) , display response in browser. tried
window.open('', '_self'); $(document).html(jqxhr.responsetext);
but blank page. suppose store error text , serve in second request server, there cleaner way?
to follow up, final code worked this:
.error(function(jqxhr, textstatus, errorthrown) { $(window).bind('unload', function() { document.write(jqxhr.responsetext); } ); var win = window.open('', '_self'); return false; });
not sure if final return false
necessary seems form.
following again: above code worked reliably in opera. thought had seen working in webkit well, started noticing wasn't; , on further testing, wasn't working firefox either.
what found worked in 3 platforms this:
document.open('text/html', true); document.write(jqxhr.responsetext); document.close();
don't have open window or bind events; re-open document , stuff text in there.
well, here again. above technique either stopped working or tripping when said ever worked @ all. chrome, in particular, doesn't seem have document.open
defined.
but! found nifty technique seems work everywhere:
errtext = 'data:text/html;base64,' + window.btoa(jqxhr.responsetext); window.open(errtext, '_self');
this converts response self-contained data:
url , opens in window.
try this:
var win = window.open('', '_self'); win.document.getelementsbytagname('body')[0].innertext = jqxhr.responsetext;
Comments
Post a Comment