Access-Control-Allow-Origin Failure with jQuery — The Result of HTTPS and Cache Buster
<p>I’m trying to run a jQuery request through the Census Bureau API, which has <code>Access-Control-Allow-Origin: "*"</code>, so it should work from anywhere. But for some pages the presence of the cache buster, e.g. <code>&amp;_=123456789</code>, results in the error <code>Origin https://... is not allowed by Access-Control-Allow-Origin</code>. If I set <code>cache: true</code>, the cache buster goes away and there is no error. Same thing on both Safari and Firefox.</p>

<p>StackOverFlow is one example: open the console from this page and run this script:</p>

<pre><code>var data_request = $.ajax({ 
        url: 'https://api.census.gov/data/2014/acs5',
        method: 'GET',
        dataType: 'json',
        cache: false,
        data: { get: 'NAME,B01001_001E', for: 'state:09,23,25,33,44,50' }
    })
    .done( function (data, textStatus, request) { 
        window.alert('Success: ' + data[1][0]);
    })
    .fail( function (request, textStatus, errorThrown) { 
        window.alert('Failure: ' + request.status + ' ' + errorThrown);
    });
</code></pre>

<p>It will fail, but if <code>cache</code> is set to <code>true</code>, it will run.</p>

<p>Any ideas?</p>

<p>— Andy</p>