开发者

jQuery 1.4.2 $.ajax with specified jsonpCallback crashes IE6/7

I posted a similiar thread before here, but after further investigation I've found that the problem is with $.ajax jsonpCallback parameter.

In the following simple code it crashes every time on the 2nd request (cache problem?)

<html>
<head>
 <title>Hello world</title>
</head>
<body>
 <span id="ClickMe">Click Me</span>
</body>
<script src="http://code.开发者_开发百科jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
 $(document).ready( function() {
  //console.log('Init');
  alert('asdasd');
  $("#ClickMe").click( function() {
   $.ajax({     
    url: 'http://URL-THAT-CAN-HANDLE-DIFFERENT-CALLBACK-NAMES',
    dataType: 'jsonp',
    jsonpCallback: 'myCustomCallback',
    success: function(data) {

    }
   });
  });

 });
</script>
</html>

This code will cause IE6 and IE7 to crash on the 2nd click on the Click Me-span.

Anyone now what can cause this?


I'm not sure what's going on here, but this kind of situation is where http://code.google.com/p/jquery-jsonp/ comes in handy :)


When you set the jsonpCallback property, jQuery adds the following code to your document, so the jsonp has something in the global scope to call. That code is something like this

window['myCustomCallBack'] = function(args){
   success(args); //really more complicated than this because success may not be in scope
   delete window['myCustomCallBack'];
}

When you call this method multiple times there is a race condition between creation of document['myCustomCallBack'] and deletion of it. It is possible for a jsonp response to come back and have myCustomCallBack deleted already, this leads to errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜