Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE wants to download returned JSON from Django

Tags:

ajax

django

I have a Django site where one page is doing an AJAX-based file upload (using Valum's file uploader) that returns some info back via JSON. The way the JSON is returned by Django is...

return HttpResponse( json.dumps( info ), mimetype="application/json" )

When trying the page in Firefox, Chrome, and Safari I get the appropriate behavior of the file uploader marking the upload as complete and the data being inserted into a table on the page.

When testing in IE8 I get incorrect behavior after Django sends the JSON back: a download dialog comes up--which is the JSON text if you save it--and the file uploader continues to think the file is uploading since it has received no response from the server. IE must be seeing the response and interpreting it as a download rather than passing it to the page's javscript. Note that I've tried changing the mime to application/javascript and this appeared to make no difference. Anyone got a fix?

like image 707
Alex Kuhl Avatar asked Dec 27 '22 23:12

Alex Kuhl


1 Answers

IE has issues with the "application/json" response from the iframe.

While I don't know the particulars of Django, I can say from experience in other frameworks that one of the easiest ways to get around this is to return the response as "text/html" and then parsing that string as JSON. In this situation I would guess it is as simple as changing the response to:

return HttpResponse( json.dumps( info ), mimetype="text/html" )

and then parsing this response whatever framework you prefer (whether it is native JSON.parse, or jQuery.parse, etc).

Should be localized only to those situations where you are ajax uploading files (as you are here).

like image 126
ddango Avatar answered Feb 01 '23 22:02

ddango