IE想下载返回的JSON从Django的IE、JSON、Django

2023-09-10 16:49:54 作者:silent 黑白年代

我有一个Django的网站,一个网页做一个基于AJAX的文件上传(使用 Valum的文件上传),返回一些信息回来通过JSON。 JSON的是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" )

当试图在火狐,Chrome和Safari的网页我得到的文件上传标记上传的完整和数据被插入到页面上的表的适当行为。

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.

当在IE8的测试我得到不正确的行为后的Django发回JSON:一个下载对话框出现时 - 也就是,如果你保存它的JSON文本 - 和文件上传仍然认为该文件被上传,因为它有没有接收到来自服务器的响应。 IE浏览器必须看到的响应和跨preting它作为一个下载,而不是把它传递给页面的javscript。请注意,我试图改变哑剧应用程序/ javascript的,这似乎没有什么区别。任何人有一个修复?

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?

推荐答案

IE浏览器有问题,应用/ JSON的从iframe的响应。

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

虽然我不知道的Django的细节,我可以从其他框架的经验说,来解决这个问题最简单的方法之一就是返回响应为text / html的,然后分析该字符串作为JSON 。在这种情况下我想这很简单,只要改变了回应:

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

和然后解析该响应任何框架你preFER(无论是天然JSON.parse,或jQuery.parse等)。

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

应该是局部的,只有那些你在哪里AJAX上传文件的情况下(如你在这里)。

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