简体   繁体   English

$ .ajax在ie6中失火

[英]$.ajax misfire in ie6

I'm using the flickr api to create a simple gallery based on images pulled in by their tag. 我正在使用flickr api基于由其标签插入的图像创建一个简单的画廊。

The gallery is working fine in every browser except ie6. 画廊在除ie6之外的所有浏览器中都可以正常工作。 When you navigate to the page (by clicking a link) in ie6, the $.ajax success/error code blocks refuse to fire, however when the page is reloaded, or navigated to directly (by entering a url) there are no problems. 当您在ie6中导航到页面(通过单击链接)时,$。ajax成功/错误代码块拒绝触发,但是,当页面重新加载或直接导航(通过输入url)时,没有问题。

$.ajax({
 type: "GET",
    url: "http://api.flickr.com/services/rest/?&method=flickr.photos.search&api_key=" + api_key + "&user_id=" + user_id + "&tags=" + tags + "&format=json&jsoncallback=?",
    cache: false,
    dataType: "jsonp",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        alert('good');
    },
    timeout: 2000,
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
    }
});

The above code is obviously a simplified version of the actual javascript, but the bug is still present. 上面的代码显然是实际javascript的简化版本,但该错误仍然存​​在。

Any help would be greatly appreciated. 任何帮助将不胜感激。 This seems to be a very weird bug. 这似乎是一个非常奇怪的错误。 Perhaps it's a caching issue? 也许这是一个缓存问题?

This problem is pretty recurrent. 这个问题经常发生。 IE caches AJAX calls. IE缓存AJAX调用。 You need to append a random number or string to your call each time so that it doesn't run a cached call. 您每次都需要在呼叫中附加一个随机数或字符串,这样它才不会运行缓存的呼叫。

url: "http://[your url]&rand=" + Math.Random(),

Two thoughts: 两个想法:

  1. Is your call within a $(document).ready() block? 您的呼叫是否在$(document).ready()块内?
  2. Try getting rid of the ampersand right at the beginning of the query string: 尝试在查询字符串的开头删除“&”号:

    /?method=flickr.photos.search&... /?method=flickr.photos.search&...

How is the "link" triggering the AJAX load? “链接”如何触发AJAX加载?

  • inline onclick="doSomething();" 内联onclick =“ doSomething();”
  • inline href="javascript:doSomething();" 内联href =“ javascript:doSomething();”
  • via jQuery $('#somelink').bind('click', doSomething); 通过jQuery $('#somelink')。bind('click',doSomething);
  • something else? 还有什么吗

I ask because IE6 has a known bug whereby if you have/use the javascript: protocol on a link... then use JavaScript to load a different page it requests the page but never renders it. 我问是因为IE6有一个已知的错误 ,即如果您拥有/使用链接上的javascript:协议...然后使用JavaScript加载其他页面,它将请求该页面,但永远不会呈现它。 I wonder if (depending on how your AJAX is attached) if the above bug extends to AJAX requests not just location.href requests. 我想知道是否(取决于您的AJAX的附加方式)上述错误是否扩展到了AJAX请求而不仅仅是location.href请求。

Four things related to your code but not to your question: 与您的代码相关但与您的问题无关的四件事:

  • timeout is not taken into account by $.ajax when dealing with jsonp 处理jsonp时,$。ajax不考虑超时
  • nor is contentType 内容类型也不是
  • the error callback is never called in the context of a jsonp request 错误回调永远不会在jsonp请求的上下文中调用
  • You should really use the data parameter (the code would be far cleaner than with the custom url building it currently shows) 您应该真正使用data参数(该代码比其当前显示的自定义网址构建要干净得多)

Point 1 & 3 are due to limitations in the way $.ajax is implemented. 第1点和第3点是由于$ .ajax实施方式的限制。 I recommend http://code.google.com/p/jquery-jsonp/ if you really need those features. 如果您确实需要这些功能,我建议http://code.google.com/p/jquery-jsonp/

Now, apart from what karim79 pointed out, I see nothing wrong with your code. 现在,除了karim79指出的内容外,我发现您的代码没有错。 So my guess is you have something else going wrong prior to it in the function you feed to $(document).ready() . 所以我的猜测是,在您提供给$(document).ready()的函数中,您之前还有其他问题。 IE is far less lenient than other browsers when it comes to javascript syntax. javascript语法方面, IE远没有其他浏览器宽容。 Try putting the $.ajax call as early as possible. 尝试尽早发出$ .ajax呼叫。

Also: 也:

  • Does it work in IE7? 可以在IE7中使用吗?
  • What version of jQuery are you using? 您正在使用哪个版本的jQuery?

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

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