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没有将其传递给脚本,所以我无法从那里执行follow up操作。
Anyone else facing this issue? Here is a snapshot.
还有人面临这个问题吗?这是一个快照。
5 个解决方案
#1
21
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.
如果有人正在使用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!
这就解决了我的问题!
#2
13
(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(对象)方法如下:
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 !)
- 不修改那些运行良好的浏览器的响应内容类型(不是<=IE9 !)
- without having to remember to use
Json(data, "text/plain")
in your various Ajax action methods - 无需记住在各种Ajax操作方法中使用Json(数据、“text/plain”)
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文件上传的消息
#3
6
I also faced this problem yesterday with WebAPI which returned a list of URLs (of asynchronously uploaded files).
昨天我也遇到了这个问题,WebAPI返回一个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”而不是默认的“application/json”;charset = utf - 8”WebAPI服务。我得到响应作为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);
});
#4
3
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".
在我的例子中,当响应头中的内容类型是“application/json;charset=UTF-8”,IE 9显示提示。但改成“text/html”后提示符不显示,虽然所有otter浏览器都可以使用“application/json”;charset = utf - 8”。
#5
2
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
#1
21
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.
如果有人正在使用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!
这就解决了我的问题!
#2
13
(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(对象)方法如下:
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 !)
- 不修改那些运行良好的浏览器的响应内容类型(不是<=IE9 !)
- without having to remember to use
Json(data, "text/plain")
in your various Ajax action methods - 无需记住在各种Ajax操作方法中使用Json(数据、“text/plain”)
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文件上传的消息
#3
6
I also faced this problem yesterday with WebAPI which returned a list of URLs (of asynchronously uploaded files).
昨天我也遇到了这个问题,WebAPI返回一个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”而不是默认的“application/json”;charset = utf - 8”WebAPI服务。我得到响应作为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);
});
#4
3
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".
在我的例子中,当响应头中的内容类型是“application/json;charset=UTF-8”,IE 9显示提示。但改成“text/html”后提示符不显示,虽然所有otter浏览器都可以使用“application/json”;charset = utf - 8”。
#5
2
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