jQuery IE8 将 json 响应视为文件并尝试下载它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8892819/
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
IE8 treats json response as file and tries to download it
提问by Gagan
I am using IE8 and I am sending ajax request to on of the url which sends back response as json. The jquery code for the ajax setup is given below:
我正在使用 IE8,我正在向 url 发送 ajax 请求,该请求将响应作为 json 发送回。ajax设置的jquery代码如下:
$(document).ready(function(){
$.ajax({
url: url_string,
dataType: "json",
success: function(response){
alert('all is well');
},
error: function(request, status, error){
alert(request);
alert(status);
alert(error);
}
});
});
I am sure that the server is sending JSON response but IE8 treats it as file and brings up download popup box. But the same process works fine for FF and Chrome. This still happens when i replace jsonto jsonpin dataType
我确信服务器正在发送 JSON 响应,但 IE8 将其视为文件并弹出下载弹出框。但同样的过程适用于 FF 和 Chrome。当我在dataType中将json替换为jsonp时,仍然会发生这种情况
But it always enters into error callback method.
但它总是进入错误回调方法。
My jsonresponse body consists of a string with html tagstoo.
我的json响应正文也包含一个带有html 标签的字符串。
Any idea why is this happening?
知道为什么会这样吗?
Thanks
谢谢
采纳答案by Andreas
I had the same issue and fixed it by setting Content-type = "text/html" in the response header for all IE requests (rather than "application/json")
我遇到了同样的问题,并通过在所有 IE 请求(而不是“application/json”)的响应头中设置 Content-type = "text/html" 来修复它
I also wrote a blog post about it with some more information: 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 Harry Forbess
Depending on what is sending the json, you have to send it as mime type text. So in rails I had to do this.
根据发送 json 的内容,您必须将其作为 mime 类型文本发送。所以在 Rails 中我不得不这样做。
render :text => my_array.to_json
Instead of
代替
render :json => my_array
回答by Nicholas Murray
I modified the url of your code and used the latest version of JQuery and it runs fine within IE8 for me
我修改了你的代码的 url 并使用了最新版本的 JQuery,它在 IE8 中对我来说运行良好
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script>
$(document).ready(function() {
$.ajax({
url: "http://api.flickr.com/services/feeds/groups_pool.gne?id=807213@N20&lang=en-us&format=json&jsoncallback=?",
dataType: "json",
success: function(response){
alert('all is well');
alert($.param(response));
},
error: function(request, status, error){
alert(request);
alert(status);
alert(error);
}
});
});
</script>
</body>
</html>
There is a known issue as detailed in this answerwhere IE8 has problems with an extra comma in a result array. Check the contents of the response alert for this.
此答案中详细说明了一个已知问题,其中 IE8 在结果数组中存在额外逗号的问题。为此检查响应警报的内容。