IE9 JSON 数据“您要打开还是保存此文件”

发布于 2024-10-24 17:53:51 字数 290 浏览 1 评论 0原文

开始使用 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.enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白芷 2024-10-31 17:53:52

事实上,你是对的@EricLaw。在 Json 结果中设置内容类型后,它就起作用了。
我必须添加以下几行:

 result.ContentEncoding = System.Text.Encoding.UTF8; 
 result.ContentType = "application/json; charset=UTF-8

Actually, you were right @EricLaw. After setting the content type in the Json result, it worked.
I had to add the following lines:

 result.ContentEncoding = System.Text.Encoding.UTF8; 
 result.ContentType = "application/json; charset=UTF-8
戏蝶舞 2024-10-31 17:53:51

如果有人正在使用 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);
}

这为我解决了这个问题!

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.

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!

尝蛊 2024-10-31 17:53:51

(最初发布的答案 此问题。)

如果使用 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);
    }
}

现在,您的控制器可以全部继承 ExtendedController 并简单地调用 return Json(model); ...

  • 而无需修改那些播放良好的浏览器(不是 <=IE9!)的响应内容类型,
  • 而不必记住使用 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:

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

  • 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

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

一身仙ぐ女味 2024-10-31 17:53:51

昨天我在使用 WebAPI 时也遇到了这个问题,它返回了一个 URL 列表(异步上传的文件)。

只需将 WebAPI 服务的内容类型设置为“text/html”而不是默认的“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);
});

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.

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);
});
最冷一天 2024-10-31 17:53:51

在我的情况下,当响应标头中的 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文