title: IE8 jquery JSON / JSON.parse error
body: <p>I'm working on some 3rd party code that we've taken over now which isn't working in IE8.  It's just an ajax call returning some json data.  Works fine in most browsers, but IE8 is throwing a syntax error.  According to the IE8 dev tools script debugger, this is the offending bit of code (this is from jquery 1.9.1.min):</p>
  
  <pre><code>return e.JSON&amp;&amp;e.JSON.parse?e.JSON.parse(n):null===n?n:"string"==typeof n&amp;&amp;(n=b.trim(n),n&amp;&amp;k.test(n.replace(S,"@").replace(A,"]").replace(E,"")))?Function("return "+n)():(b.error("Invalid JSON: "+n),t)
  </code></pre>
  
  <p>I thought for sure that adding <strong>json2.js</strong> would solve this as I know IE8 has some json support issues.  Adding the json2 lib didn't seem to make any difference.  I also made sure IE8 wasn't in compat mode by adding this to the head:</p>
  
  <pre><code>&lt;meta http-equiv="X-UA-Compatible" content="IE=EDGE" /&gt;
  </code></pre>
  
  <p>As I'm pretty sure that can cause some json issues as well.</p>
  
  <p>Project is using <strong>jquery 1.9.1</strong> if that matters, though I don't think that it does for this issue.  I'm pretty sure 1.9 dropped $.browser support but it doesn't look like that's being used in this code base.</p>
  
  <p>The json returned is valid as well.</p>
  
  <p>Anyone have any ideas?</p>
  
  <hr>
  
  <p><strong>EDIT</strong>  Here's the code.  The first alert is undefined in IE8.</p>
  
  <pre><code>Geocode = function (address, state) {
  var url = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&amp;address=';
  var param = encodeURIComponent(address + ', ' + state);
  
  var result = jQuery.ajax({
      url: url + param,
      type: "GET",
      async: false,
      dataType: 'json'
  });
  
  alert(result.responseText); //undefined in ie8
  var json = jQuery.parseJSON(result.responseText);
  alert(json.results[0].geometry.location.lat);
  
  if (json.status != 'OK') {
      alert('Unable to determine the location of the city and state you entered');
      return null;
  }
  
  return json.results[0].geometry.location;
  </code></pre>
  
  <p>};</p>
  
  <p><strong>EDIT 2</strong> It looks like the issue is IE8-9's partial support for cross domain requests.  The following is some stripped down code I played with last night where my requests succeed.  The code won't match what's above, but basically you need to use a XDomainRequest:</p>
  
  <pre><code>var protocol = location.protocol,
      url = '//maps.googleapis.com/maps/api/geocode/json?sensor=false&amp;address=',
      city = 'san francisco',
      state = 'california',
      address = encodeURIComponent(city + ', ' + state);
  
  /**********************************************************
  
  for cross domain requests in ie8-9 (partial CORS support), use special XDR (XDomainRequest), but it does have limitations
  http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
  
      1. The target URL must be accessed using the HTTP or HTTPS protocols
      2. The target URL must be accessed using only the HTTP methods GET and POST
      3. No custom headers may be added to the request
      4. Only text/plain is supported for the request's Content-Type header
      5. No authentication or cookies will be sent with the request
      6. Requests targeted to Intranet URLs may only be made from the Intranet Zone
      7. Requests must be targeted to the same scheme as the hosting page
  
  ***********************************************************/
  if ('XDomainRequest' in window &amp;&amp; window.XDomainRequest !== null) {
      //IEs that do not support cross domain xhr requests
      var xdr = new XDomainRequest();
  
      xdr.open('get', protocol + url + address);
      xdr.onload = function() {
          var data = $.parseJSON(xdr.responseText);
          outputData(data);
      };
      xdr.send();
  } else {
      //good browsers
      $.ajax({
          url: protocol + url + address,
          type: 'get',
          dataType: 'json',
          success: function(data){
              outputData(data);
          }
      });
  }
  
  function outputData(data) {
      $('body')
          .append('&lt;p&gt;status: ' + data.status + '&lt;/p&gt;')
          .append('&lt;p&gt;latitude: ' + data.results[0].geometry.location.lat + '&lt;/p&gt;');
  }
  </code></pre>