views:

123

answers:

3

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?

+2  A: 
Pointy
I've just tried putting my new HTML string all on one line... no joy I'm afraid
scrumpyjack
A: 

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:

http://stackoverflow.com/questions/1284163/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.

treeface
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
scrumpyjack
Why are you using alerts and not the IE8 debugger?
Pointy
@Pointy how do I use the IE8 debugger?
scrumpyjack
Hm...can you possibly echo back the json_encode value? Maybe we can figure it out from there.
treeface
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
+3  A: 

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!

scrumpyjack
wow, I would simply never have guessed that :-)
Pointy