IE9 JSON 数据“您要打开还是保存此文件”
开始使用 IE9 测试我的 jQuery 应用程序。看来我在这里可能遇到麻烦了。 我注意到,当我将 JSON 数据返回到 Javascript 方法时,我总是收到这样的提示:“您想打开或保存此文件吗?”并为我提供了 3 个按钮:打开、保存和取消。当然,我的 javascript 正在根据 JSON 对象中设置的值采取操作,但由于 IE9 没有将其传递给脚本,因此我无法从那里执行后续操作。
还有其他人面临这个问题吗?这是一个快照。
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.
Anyone else facing this issue? Here is a snapshot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事实上,你是对的@EricLaw。在 Json 结果中设置内容类型后,它就起作用了。
我必须添加以下几行:
Actually, you were right @EricLaw. After setting the content type in the Json result, it worked.
I had to add the following lines:
如果有人正在使用 ASP.net MVC 并尝试解决此问题 - 我在 MVC 框架中使用了以下内置方法。只需更新 JsonResult 上的内容类型和编码即可。
这为我解决了这个问题!
If anyone is using ASP.net MVC and 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.
This fixed the issue for me!
(最初发布的答案 此问题。)
如果使用 MVC,处理此问题的一种方法是实现一个基本控制器,您可以在其中覆盖(隐藏)Json(object) 方法,如下所示:
现在,您的控制器可以全部继承 ExtendedController 并简单地调用
return Json(model);
...Json(data, "text/plain")
在您的各种 Ajax 操作方法中这适用于 json 请求,否则会在 IE8 和 IE8 中显示“打开或保存”消息。 IE9,例如 jQuery 文件上传
(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:
Now, your controllers can all inherit ExtendedController and simply call
return Json(model);
...Json(data, "text/plain")
in your various Ajax action methodsThis works with json requests which would otherwise display the "Open or Save" message in IE8 & IE9 such as those made by jQuery File Upload
昨天我在使用 WebAPI 时也遇到了这个问题,它返回了一个 URL 列表(异步上传的文件)。
只需将 WebAPI 服务的内容类型设置为“text/html”而不是默认的“application/json; charset=UTF-8”即可。我收到 JSON 字符串形式的响应,然后使用 $.parseJSON 将其转换为 JSON 对象。
I also faced this problem yesterday with WebAPI which returned a list of URLs (of asynchronously uploaded files).
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.
在我的情况下,当响应标头中的 contentType 为“application/json; charset=UTF-8”时,IE 9 显示提示。但是更改为“text/html”则提示不显示,尽管所有水獭浏览器都可以使用“application/json; charset=UTF-8”。
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".