最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $.getJSON (jQuery) not working in IE 8 - Stack Overflow

programmeradmin3浏览0评论

There is some straightforward AJAX code that works in Firefox and Chrome but not in IE. I am not doing anything fancy but the code is too long to post here.

Essentially, it is:

<script type="text/javascript">
var baseurl = '/';
 var setUpGame = function(lvl){
  var ajaxurl;
  ajaxurl = baseurl+'ajax.php?f=gg&l='+lvl;
  $.getJSON(ajaxurl,function(data){
                   //do stuff with data here
  });
 };
 $(document).ready(function(){
  setUpGame(3);
 });
</script>  

The problem is that IE has a problem with $.getJSON. The call stack shows that it fails in the bowels of jQuery (Line 123, column 183) where the jQuery code reads "return new A.XMLHttpRequest"

The error message is "Error: Object doesn't support this property or method". I have verified that the ajaxurl variable has the right value and that URL returns the correct JSON.

Could this be happening because all this is running inside an iFrame?

There is some straightforward AJAX code that works in Firefox and Chrome but not in IE. I am not doing anything fancy but the code is too long to post here.

Essentially, it is:

<script type="text/javascript">
var baseurl = 'http://mydomain./facebook/';
 var setUpGame = function(lvl){
  var ajaxurl;
  ajaxurl = baseurl+'ajax.php?f=gg&l='+lvl;
  $.getJSON(ajaxurl,function(data){
                   //do stuff with data here
  });
 };
 $(document).ready(function(){
  setUpGame(3);
 });
</script>  

The problem is that IE has a problem with $.getJSON. The call stack shows that it fails in the bowels of jQuery (Line 123, column 183) where the jQuery code reads "return new A.XMLHttpRequest"

The error message is "Error: Object doesn't support this property or method". I have verified that the ajaxurl variable has the right value and that URL returns the correct JSON.

Could this be happening because all this is running inside an iFrame?

Share Improve this question edited Jul 1, 2010 at 4:37 tinkerr asked Jun 30, 2010 at 23:58 tinkerrtinkerr 1,0582 gold badges16 silver badges35 bronze badges 3
  • Try using a relative url instead of the base url... I've had some trouble with this in the past. – Mottie Commented Jul 1, 2010 at 1:18
  • Relative URL did not work :( When I debug using IE's script debugger, it shows me the ajaxurl variable in red. The value of the variable is correct and the data type is string. But the value is shown in red. Does that help? – tinkerr Commented Jul 1, 2010 at 1:52
  • Drop a breakpoint in the code where you're seeing this and inspect all variables to make sure they are what you think they are. What is the datatype and value of ajaxurl at the point in the code where it's red? Also, just make sure that the AJAX request in the iframe is going to the same domain (exactly the same domain) as the iframe document. – Andrew Commented Jul 1, 2010 at 4:45
Add a ment  | 

2 Answers 2

Reset to default 4

Took me hours to figure this out, so hopefully I can save someone else the headache.

As noted elsewhere, IE doesn't use XMLHttpRequest.

However, many of the solutions posted elsewhere don't work for me.

I tried:

  1. jQuery.support.cors = true; But that doesn't seem to do anything.

  2. JSONP would work if this were a singleton function. But I'm working inside a relatively plex Object that need multiple instances on a single page, but callbacks only really work in the global name space, and I wind up with collisions...very ugly collisions.

  3. $.getScript won't work for me because I need to actually massage the json data that gets returned. And while the 'script' dataType doesn't throw a XDomain access error, it also doesn't return the actual data to my .success callback function.

  4. $.getJSON doesn't work for IE8 - it throws an "Access Denied" error. Basically runs into CORS issues. And nothing on a global level seems to help.

So what I found works is the following:

$.ajax({ 
    url : remote_cross_domain_non_local_url,
    dataType : 'jsonp'
})
.success( function(data){ dosomethingwithdata(data); } );

It's kind of a hack, because I'm specifying a jsonp datatype even though there's no wrapper function in the data that es back. But in order to get around the XDomain issues with IE, we have to fool it into thinking we're getting a JSONP script.

I switched from jQuery 1.4.2 to 1.3.2 and this problem disappeared.

发布评论

评论列表(0)

  1. 暂无评论