jQuery AJAX 请求在 IE 中失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/425854/
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
jQuery AJAX request failing in IE
提问by Sam
The following AJAX call is failing in IE.
以下 AJAX 调用在 IE 中失败。
$.ajax({
url:"{{SITE_URL}}/content/twitter.json",
dataType:"json",
error:function(xhr, status, errorThrown) {
alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
},
success:function(json) {
...Snip...
}
});
The error function returns
错误函数返回
Undefined
parsererror
OK
No request is made to the server so I don't think its a problem with the JSON.
没有向服务器发出请求,所以我认为 JSON 没有问题。
Fixed, See #1351389
已修复,请参阅 #1351389
回答by Sam
Fixed, I changed the content-type from application/json; charset=utf8
to just plain application/json
.
I hate IE :)
已修复,我将内容类型从 更改为application/json; charset=utf8
纯application/json
.
我讨厌 IE :)
Also to avoid IE super-caching try this:
同样为了避免 IE 超级缓存,试试这个:
var d = new Date();
$.ajax({
url:"{{SITE_URL}}/content/twitter.json?_="+d.getTime(),
...Snip...
That way each request is a new url for IE to get :D
这样每个请求都是 IE 获取的新 url :D
回答by tanathos
For the caching problem why don't you simple use the cache: false
parameter?
对于缓存问题,为什么不简单地使用cache: false
参数?
$.ajax({
url: "yoururl",
cache: false,
....
回答by Javier
is this a copy/paste? the one thing that gets me all the time is leaving the last ',' in an object constructor. that is, most browsers JS accept:
这是复制/粘贴吗?一直让我着迷的一件事是在对象构造函数中留下最后一个“,”。也就是说,大多数浏览器 JS 都接受:
o = { a:1, b:2, c:3, };
but IE chokes on this because the comma after the last item. change it to:
但 IE 对此感到窒息,因为最后一项之后的逗号。将其更改为:
o = { a:1, b:2, c:3 };
and it works.
它有效。
回答by Jav_Rock
In newer versions of internet explorer (IE7) it is necessary to write the next line before calling $.ajax, otherwise it would never call the function:
在较新版本的 Internet Explorer (IE7) 中,必须在调用$.ajax之前编写下一行,否则它永远不会调用该函数:
$.ajaxSetup({ cache: false }); //this line before $.ajax!!!
$.ajax({
//codes
//codes
//codes
});
回答by Craig Stuntz
IE caches AJAX requests really aggressively (more so than Firefox, anyway). You need to set the Cache-Control headers in the response appropriately if this is not right for your site.
IE 非常积极地缓存 AJAX 请求(无论如何比 Firefox 更是如此)。如果这不适合您的站点,您需要在响应中适当地设置 Cache-Control 标头。
回答by Luca Matteis
One major problem with statically generated JSON and IE are the leading "commas", for examples this throws an error in IE:
静态生成的 JSON 和 IE 的一个主要问题是前导“逗号”,例如这会在 IE 中引发错误:
{
"one":"hello",
"two":"hi",
}
Note the last comma.
注意最后一个逗号。
回答by Pim Jager
What is the {{SITE_URL}} chunk giving is about. Try looking at the code in view source code of the browser. If the {{SITE _URL}} chunk has a trailing slash and that would make the request url:
给出的 {{SITE_URL}} 块是关于什么的。尝试在浏览器的查看源代码中查看代码。如果 {{SITE _URL}} 块有一个尾部斜杠,这将使请求 url:
http://modomain.com//content/twitter.json
Which could creep IE out?
哪个可以让 IE 消失?
回答by user333483
IE: JSON not defined error resolved at
IE:JSON 未定义错误已解决
http://funkatron.com/site/comments/safely-parsing-json-in-javascript/
http://funkatron.com/site/comments/safely-parsing-json-in-javascript/
by using dataType: "json" and avoid parsing
通过使用 dataType: "json" 并避免解析