5

I've got a very standard AJAX request:

$.getJSON('/products/findmatching/38647.json', {}, function(JsonData){
  var tableHtml = '';
  var x;

  for (x in JsonData.matchingProds) {
    var matchingProd = JsonData.matchingProds[x];
    var buyMessage;

    if ( x == 0 ) {
      buyMessage = 'Buy Cheapest';
    }
    else {
      buyMessage = 'Buy from this shop';
    }

    tableHtml = tableHtml + '<tr><td><img height="40" src="' + matchingProd.img_url + '" alt="' + matchingProd.name + '" /></td> \
      <td><a href="' + matchingProd._page_url + '">' + matchingProd.name + '</a></td> \
      <td><a href="' + matchingProd._merchant._url + '">' + matchingProd._merchant.title + '</td> \
      <td align="right">&pound;' + matchingProd.price + '</td> \
      <td><a href="' + matchingProd.referral_url + '">' + buyMessage + '</a></td></tr>';
  }

  $('#matchingproducts tbody').html(tableHtml);

  $('#loading').delay(1000).fadeOut();
});

It works fine in all browsers except IE. I don't do much in IE anymore as I have a Mac, but I've got IE8 on an XP virtual machine. Using its built-in Debug Tools hasn't really helped (they're not very good). The only thing I can fathom is that at some point I get and "Expected identifier" error.

Could this be in the JSON data that's returned? How can I examine that data from IE's point of view?

simonhamp
  • 3,078
  • 1
  • 20
  • 26
  • Can you provide a sample on the site that we can peruse? Have you tried it in Chrome? If so, what errors show up (if any) after the script runs when you open up the element inspector? What does the raw JSON look like? – treeface Aug 18 '10 at 23:48
  • Sounds like you have a trailing comma in your JSON, give it a look and see if that's the case. – Nick Craver Aug 18 '10 at 23:55
  • @treeface added a link for you to take a look at – simonhamp Aug 18 '10 at 23:56
  • @Nick Craver no trailing commas. This JSON is built by PHPs json_encode function – simonhamp Aug 18 '10 at 23:57
  • When I set up a test using a copy of your exact JSON body, *and* your exact handler, it works just fine ... – Pointy Aug 19 '10 at 01:12
  • I can't figure out what's changed, but if it's really working I'm happy for you :-) – Pointy Aug 19 '10 at 01:25

5 Answers5

16

Ok I figured it out. Someone suggested trying a non-minified version of jQuery. I did this and stepped through the IE8s Javascript debugger. At a certain point, the following error came up:

Could not complete the operation due to error c00ce56e.

A little Googling found that it was the charset declaration I've set for my JSON data. In PHP, this was done with:

header ( 'Content-Type: text/javascript; charset=utf8' );

It turns out that IE is very particular about the charset reference ( http://forums.asp.net/t/1345268.aspx#2732852 ), so I changed it to:

header ( 'Content-Type: text/javascript; charset=UTF-8' );

And hey-presto, it works like a charm. Thanks for your help guys, you pointed me in the right direction again!

simonhamp
  • 3,078
  • 1
  • 20
  • 26
  • 1
    Mother-effin-MS... thank you sir, this probably saved me hours. – Peter Kruithof Aug 01 '11 at 11:56
  • 2
    On second thought, this is according to the IANA standard (http://en.wikipedia.org/wiki/UTF-8#Official_name_and_variants), IE actually gets it right here. The question if they should be liberal about accepting other variants is a different debate ;) – Peter Kruithof Aug 01 '11 at 12:10
  • @PeterKruithof given how people always attack IE for not being strict in standards enforcement (which is simply wrong) there's really not much they can do to please. Do or don't, people attack them either way. – jwenting Mar 06 '14 at 07:58
2

edit again — still debugging - that change to use the other function needs to have the last argument be myAjaxResponderFunc with no quotes ...

Pointy
  • 405,095
  • 59
  • 585
  • 614
2
$.ajaxSetup({ cache: false }); 
John Conde
  • 217,595
  • 99
  • 455
  • 496
ADL
  • 2,707
  • 1
  • 16
  • 23
1

You have to use check browser and version for IE8+, then use the XDomainRequest() if msie8+.

This will return a JSON String, must use jQuery.parseJSON() to create the JSON object…

Don't use getJSON!

Here's my example:

if ($.browser.msie && parseInt($.browser.version, 10) >= 8 && window.XDomainRequest) {
        // Use Microsoft XDR
        var xdr = new XDomainRequest();
        xdr.open("get", reqURL);
        xdr.onload = function() {
            var json = xdr.responseText;
            json = $.parseJSON(json);

            $.each(json.results, function(i, val) {
                    console.log(val.formatted_address);
                    var locString = val.formatted_address;
                    $.each(val.address_components, function(x, comp) {

                        if($.inArray("postal_code", comp.types) > -1) {
                            //alert("___" + comp.types);
                            zipmap[locString] = comp.short_name;
                        }

                    });

                    suggestions.push(val.formatted_address);
                });

            //alert(json.results);
        }
        xdr.send();
        add(suggestions); 
    }else {
        $.getJSON(reqURL, function(data) {

            var isZIP = new Boolean;
            console.log(data.results);
            $.each(data.results, function(i, val) {
                console.log(val.formatted_address);
                var locString = val.formatted_address;
                $.each(val.address_components, function(x, comp) {

                    if($.inArray("postal_code", comp.types) > -1) {
                        console.log("___" + comp.types);
                        zipmap[locString] = comp.short_name;
                    }

                });

                suggestions.push(val.formatted_address);
            });

            add(suggestions);  

        });
    }

requrl is the URL which you are making a request to.

Done!

Credit to: http://graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/

I just LOVE IE!

jmarcosSF
  • 69
  • 1
  • 8
  • 1
    Using an `XDomainRequest()` is the proper way to do this in early versions of IE. Best of luck to all who end up here! – DrewT Sep 11 '14 at 22:25
0

Hm...it appears that your script is running fine in IE. The only thing that appears to be breaking is your jQuery fadeOut method. I was able to find something about that here:

jquery IE Fadein and Fadeout Opacity

Basically, IE has issues with altering CSS properties when they haven't previously been declared.

Edit: Perhaps it is not running fine in IE...I might not have understood the exact process of the page load.

Community
  • 1
  • 1
treeface
  • 13,270
  • 4
  • 51
  • 57
  • Sorry, it's definitely the getJSON function that's failing as it's not updating the HTML. I've even just put a simple alert in the callback and I get nothing. It's definitely getJSON that's not working. But I will bear this in mind, thanks – simonhamp Aug 19 '10 at 00:32
  • Why are you using alerts and not the IE8 debugger? – Pointy Aug 19 '10 at 00:49
  • @Pointy how do I use the IE8 debugger? – simonhamp Aug 19 '10 at 00:51
  • Hm...can you possibly echo back the json_encode value? Maybe we can figure it out from there. – treeface Aug 19 '10 at 00:52
  • Well you just open up the "Developer" tools and then click the main 'Script" tab, and then click "Start Debugging". You should be able to set a breakpoint at the getJSON call, or at the first line of the callback. Your JSON looks fine and (notwithstanding my incorrect comment about the long string) the code looks fine too. – Pointy Aug 19 '10 at 01:13