IE9 JSON 数据“您要打开还是保存此文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5388893/
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
IE9 JSON Data "do you want to open or save this file"
提问by Anup Marwadi
Started testing my jQuery applications with IE9. Looks like I may be in for some trouble here. I noticed that when I return JSON data back to the Javascript methods I always get this Prompt that says: "Do you want to open or save this file?" and provides me with 3 buttons: Open, Save and Cancel. Of course, my javascript is taking actions based on the values set in the JSON object but since IE9 doesn't pass it over to the script, I cannot execute the follow up action from there on.
开始使用 IE9 测试我的 jQuery 应用程序。看来我在这里遇到了一些麻烦。我注意到当我将 JSON 数据返回给 Javascript 方法时,我总是得到这个提示:“你想打开还是保存这个文件?” 并为我提供了 3 个按钮:打开、保存和取消。当然,我的 javascript 正在根据 JSON 对象中设置的值执行操作,但由于 IE9 没有将其传递给脚本,因此我无法从那里执行后续操作。
Anyone else facing this issue? Here is a snapshot.
还有其他人面临这个问题吗?这是一个快照。
采纳答案by Anup Marwadi
Actually, you were right @EricLaw. After setting the content type in the Json result, it worked. I had to add the following lines:
实际上,您是对的@EricLaw。在 Json 结果中设置内容类型后,它起作用了。我不得不添加以下几行:
result.ContentEncoding = System.Text.Encoding.UTF8;
result.ContentType = "application/json; charset=UTF-8
回答by Deano
If anyone is using ASP.net MVCand trying to fix this issue - I used the following built in methods in the MVC framework. Simply update the content Type and encoding on the JsonResult.
如果有人正在使用ASP.net MVC并试图解决这个问题 - 我在MVC 框架中使用了以下内置方法。只需更新JsonResult上的内容类型和编码。
public ActionResult Index(int id)
{
// Fetch some data
var someData = GetSomeData();
// Return and update content type and encoding
return Json(someData, "text/html", System.Text.Encoding.UTF8,
JsonRequestBehavior.AllowGet);
}
This fixed the issue for me!
这为我解决了问题!
回答by Chris
(Answer originally posted for this question.)
(最初为这个问题发布的答案。)
If using MVC, one way of handling this is to implement a base controller in which you override(hide) the Json(object) method as follows:
如果使用 MVC,处理此问题的一种方法是实现一个基本控制器,您可以在其中覆盖(隐藏)Json(object) 方法,如下所示:
public class ExtendedController : Controller
{
protected new JsonResult Json(object data)
{
if (!Request.AcceptTypes.Contains("application/json"))
return base.Json(data, "text/plain");
else
return base.Json(data);
}
}
Now, your controllers can all inherit ExtendedController and simply call return Json(model);...
现在,您的控制器都可以继承 ExtendedController 并简单地调用return Json(model);...
- without modifying the response content type for those browsers which play nicely (not <=IE9 !)
- without having to remember to use
Json(data, "text/plain")in your various Ajax action methods
- 无需修改那些运行良好的浏览器的响应内容类型(不是 <=IE9 !)
- 无需记住
Json(data, "text/plain")在您的各种 Ajax 操作方法中使用
This works with json requests which would otherwise display the "Open or Save" message in IE8 & IE9 such as those made by jQuery File Upload
这适用于 json 请求,否则会在 IE8 和 IE9 中显示“打开或保存”消息,例如jQuery 文件上传
回答by Tien Do
I also faced this problem yesterday with WebAPI which returned a list of URLs (of asynchronously uploaded files).
我昨天也遇到了这个问题,它返回了一个 URL 列表(异步上传的文件)。
Just set content type to "text/html" instead of default "application/json; charset=UTF-8" of WebAPI services. I got response as a JSON string and then used $.parseJSON to convert it to JSON object.
只需将内容类型设置为“text/html”,而不是 WebAPI 服务的默认“application/json; charset=UTF-8”。我得到了 JSON 字符串的响应,然后使用 $.parseJSON 将其转换为 JSON 对象。
public async Task<HttpResponseMessage> Upload()
{
// ...
var response = Request.CreateResponse(HttpStatusCode.OK, files);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
// result is an iframe's body content that received response.
$.each($.parseJSON(result.html()), function (i, item)
{
console.log(item.Url);
});
回答by Rich
In my case when contentType in response header is "application/json; charset=UTF-8", the IE 9 shows that Prompt. But changed to "text/html" then the prompt does not show, although all otter browsers are fine with the "application/json; charset=UTF-8".
在我的情况下,当响应标头中的 contentType 是“application/json; charset=UTF-8”时,IE 9 显示该提示。但是更改为“text/html”然后不显示提示,尽管所有 otter 浏览器都可以使用“application/json; charset=UTF-8”。

