IE中的Json响应下载(7~10)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13943439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:40:15  来源:igfitidea点击:

Json response download in IE(7~10)

jsoninternet-explorerc#-4.0asp.net-mvc-4http-headers

提问by smehnawal

I am trying to upload a file and return a json response regarding properties(name, size etc) of the file. It works fine in all browsers except IE.

我正在尝试上传文件并返回有关文件属性(名称、大小等)的 json 响应。它适用于除 IE 之外的所有浏览器。

IE tries to download the JSON as a file !

IE 尝试将 JSON 作为文件下载!

I have IE10 and testing it on IE7 to 10 by changing browser mode and document mode from the debugger.

我有 IE10 并通过从调试器更改浏览器模式和文档模式在 IE7 到 10 上对其进行测试。

I am using asp.net mvc4, the file upload action have HttpPost attribute and i am returning json response using return Json(myObject);

我正在使用 asp.net mvc4,文件上传操作具有 HttpPost 属性,我使用return Json(myObject);返回 json 响应

And here are my http headers

这是我的 http 标头

Request

要求

Key Value
Request POST /File/UploadFile/ HTTP/1.1
Accept  text/html, application/xhtml+xml, */*
Referer http://localhost:63903/
Accept-Language en-NZ
User-Agent  Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type    multipart/form-data; boundary=---------------------------7dc1e71330526
Accept-Encoding gzip, deflate
Host    localhost:63903
Content-Length  1377002
DNT 1
Connection  Keep-Alive
Cache-Control   no-cache

Response

回复

Key Value
Response    HTTP/1.1 200 OK
Server  ASP.NET Development Server/11.0.0.0
Date    Tue, 18 Dec 2012 23:44:19 GMT
X-AspNet-Version    4.0.30319
X-AspNetMvc-Version 4.0
Cache-Control   private
Content-Type    application/json; charset=utf-8
Content-Length  154
Connection  Close

I tried a few suggestions but so far back to square one !

我尝试了一些建议,但到目前为止回到第一个!

回答by Gabriele Petrioli

You will need to return the json as text/htmlsince IE does not know what to do with application/jsoncontents..

您将需要返回 json,text/html因为 IE 不知道如何处理application/json内容..

return Json(myObject, "text/html");


Not sure but it might work (and it would be more correct if it does) to use text/x-json

不确定,但它可能会工作(如果这样做会更正确)使用text/x-json

return Json(myObject, "text/x-json");

回答by Hyman

Even though this question is a few months old, I thought I'll add one more suggestion, just in case anyone else is using ASP.NET MVC 3 or 4and runs into this problem.

尽管这个问题已经有几个月的历史了,但我想我会再添加一个建议,以防万一其他人正在使用ASP.NET MVC 3 or 4并遇到这个问题。

In my experience, when IE attempts to download the Json response as a file all you have to do to correct the problem is to add a reference to jquery.unobtrusiveto your view.

根据我的经验,当 IE 尝试将 Json 响应下载为文件时,您只需添加jquery.unobtrusive对视图的引用即可纠正问题。

for example:

例如:

@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")

Once this is in place IE will no longer try to download the json response from a JsonResult controller action. No need to change the response type etc..

一旦到位,IE 将不再尝试从 JsonResult 控制器操作下载 json 响应。无需更改响应类型等。