4

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?

Alex Kuhl
  • 1,814
  • 1
  • 13
  • 19
  • 1
    IE doesn't handle iframes the same way as Firefox, Chrome, Safari, and Opera. You'll have to make a work around for the 'importNode' method which isn't supported in IE. That's about as far as I was willing to fix/investigate until I just decided to use SWFUpload. – jbcurtin Mar 17 '11 at 14:20
  • 1
    Oh that's right, the iframe fallback. I may end up using some other method for – Alex Kuhl Mar 17 '11 at 14:43

2 Answers2

10

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).

ddango
  • 946
  • 1
  • 12
  • 25
0

Try setting a mime-type of "text/plain", that should work. See this question for more details:

json xhr response opens a download file popup window

There you can also find in the last comment why setting mime-type to text/html is a bad idea (opens your site to XSS attacks)

Community
  • 1
  • 1
Teodor Sandu
  • 1,348
  • 1
  • 20
  • 31