简体   繁体   English

IE中的jQuery AJAX请求失败

[英]jQuery AJAX request failing in IE

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

Fixed, I changed the content-type from application/json; charset=utf8 修复了,我从application/json; charset=utf8更改了内容类型application/json; charset=utf8 application/json; charset=utf8 to just plain application/json . application/json; charset=utf8只是简单的application/json
I hate IE :) 我讨厌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

For the caching problem why don't you simple use the cache: false parameter? 对于缓存问题,为什么不简单地使用cache: false参数?

$.ajax({ 
    url: "yoururl",
    cache: false,
    ....

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. 但是因为最后一项之后的逗号,IE会因此而窒息。 change it to: 将其更改为:

o = { a:1, b:2, c:3 };

and it works. 它的工作原理。

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

IE caches AJAX requests really aggressively (more so than Firefox, anyway). IE非常积极地缓存AJAX请求(无论如何都比Firefox更强)。 You need to set the Cache-Control headers in the response appropriately if this is not right for your site. 如果这不适合您的站点,则需要在响应中适当设置Cache-Control标头。

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. 注意最后一个逗号。

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”并避免解析

What is the {{SITE_URL}} chunk giving is about. 什么是{{SITE_URL}}大块给出的是什么。 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}}块有一个尾部斜杠,那将使请求url成为:

http://modomain.com//content/twitter.json

Which could creep IE out? 哪个可以蠕变IE?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM