jQuery IE 提示打开或保存来自服务器的 json 结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6114360/
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
IE prompts to open or save json result from server
提问by iLemming
Internet explorer in compatibility mode gets the data from the server in an ajax callback method, and pops-up a dialog if I want to save the data or open. How to get rid of that?
兼容模式下的浏览器通过ajax回调的方式从服务器获取数据,如果我想保存数据或打开,会弹出一个对话框。如何摆脱它?
client says:
客户说:
$.ajax({
type:'POST',
data: $("#UIdlgHolder > form").serialize(),
url: $("#UIdlgHolder > form").attr("action"),
success: function (data, textStatus, jqXHR) {
{
alert(data.message);
}
}
server answers:
服务器回答:
return new JsonResult { Data = new { result = false, message = "Yay!" } };
采纳答案by Cargowire
Sounds like this SO question may be relevant to you:
听起来这个SO问题可能与您有关:
How can I convince IE to simply display Application json rather than offer to download
我怎样才能说服 IE 简单地显示应用程序 json 而不是提供下载
If not:
如果不:
Have you tried setting the dataType expected in the ajax options? i.e. dataType: 'json'
您是否尝试过在 ajax 选项中设置预期的数据类型?即数据类型:'json'
Have you tried other content types such as 'application/json' or 'text/javascript'
您是否尝试过其他内容类型,例如“application/json”或“text/javascript”
回答by ItsJason
Even though it's not supposedly the correct way, setting the content type to text/html made IE deal with this correctly for me:
尽管这不是正确的方法,但将内容类型设置为 text/html 使 IE 为我正确处理:
return Json(result, "text/html");
Works in all the version that F12 tools gives you in IE9.
适用于 F12 工具在 IE9 中为您提供的所有版本。
回答by Chris
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 Mike Gledhill
Sadly, this is just another annoying quirk of using Internet Explorer.
遗憾的是,这只是使用 Internet Explorer 的另一个恼人的怪癖。
The simple solution is to run a small .reg file on your PC, to tell IE to automaticallyopen .json files, rather than nag about whether to open/save it.
简单的解决方案是在您的 PC 上运行一个小的 .reg 文件,告诉 IE自动打开 .json 文件,而不是唠叨是否打开/保存它。
I've put a copy of the file you'll need here:
我已经把你需要的文件的副本放在这里:
You'll need to have Admin rights to run this.
您需要具有管理员权限才能运行它。
回答by Andreas
I changed the content-type to "text/html" instead of "application/json" server side before returning the response. Described it in a blog post, where other solutions have also been added:
在返回响应之前,我将内容类型更改为“text/html”而不是“application/json”服务器端。在博客文章中对其进行了描述,其中还添加了其他解决方案:
http://blog.degree.no/2012/09/jquery-json-ie8ie9-treats-response-as-downloadable-file/
http://blog.degree.no/2012/09/jquery-json-ie8ie9-treats-response-as-downloadable-file/
回答by Tomasz Miku?
Have you tried to send your ajax request using POST method ? You could also try to set content type to 'text/x-json' while returning result from the server.
您是否尝试过使用 POST 方法发送 ajax 请求?您还可以尝试在从服务器返回结果时将内容类型设置为“text/x-json”。
回答by Tomasz Miku?
Is above javascript code the one you're using in your web application ? If so - i would like to point few errors in it: firstly - it has an additional '{' sign in definition of 'success' callback function secondly - it has no ')' sign after definition of ajax callback. Valid code should look like:
上面的 javascript 代码是您在 Web 应用程序中使用的代码吗?如果是这样-我想指出其中的一些错误:首先-它在“成功”回调函数的定义中具有附加的“{”符号,其次-在定义ajax回调之后没有“)”符号。有效代码应如下所示:
$.ajax({
type:'POST',
data: 'args',
url: '@Url.Action("PostBack")',
success: function (data, textStatus, jqXHR) {
alert(data.message);
}
});
try using above code - it gave me 'Yay' alert on all 3 IE versions ( 7,8,9 ).
尝试使用上面的代码 - 它在所有 3 个 IE 版本( 7,8,9 )上给了我'Yay'警报。
回答by Rael Gugelmin Cunha
I faced this while using jQuery FileUpload plugin.
我在使用jQuery FileUpload 插件时遇到了这个问题。
Then I took a look in their documentation, most exactly in the Content-Type Negotiation sectionand followed their suggestion for Ruby/Rails.
然后我查看了他们的文档,最准确的是在Content-Type Negotiation 部分,并遵循了他们对Ruby/Rails的建议。
render(json: <some-data>, content_type: request.format)
Which fixed the issue for me.
这为我解决了这个问题。
Quick Explanation: for old IE/Opera versions, this plugin will use an iframe with text/plain
or text/html
content-type, so if you force the response to json
, browser will try download it. Using the same content-type as in the request will make it work for any browser.
快速说明:对于旧的 IE/Opera 版本,此插件将使用带有text/plain
或text/html
内容类型的 iframe ,因此如果您强制响应为json
,浏览器将尝试下载它。使用与请求中相同的内容类型将使其适用于任何浏览器。
回答by Artur Beljajev
In my case, IE11 seems to behave that way when there is some JS syntax error in the console (doesn't matter where exactly) and dataType: 'json'
has no effect at all.
就我而言,IE11 似乎在控制台中出现一些 JS 语法错误时(与确切位置dataType: 'json'
无关)时表现得那样,并且根本没有任何影响。