Aman Feyzi
Aman Feyzi

Reputation: 35

AJAX request not working in IE8 and IE9

This AJAX code is working in IE10+ and Chrome and another browser, but it is not working in IE8 and IE9.

<table id="table" border="1"> 
    <tbody style="display: table-row-group"></tbody>
</table>

jQuery(document).ready(function(){
    $.support.cors = true;

    var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22EURUSD%22%2C%20%22CADUSD%22%2C%20%22GBPUSD%22%2C%20%22AEDUSD%22%2C%20%22TRYUSD%22%2C%20%22RUBUSD%22%2C%20%22INRUSD%22%2C%20%22SARUSD%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";

    var $tbody = $('#table').find('tbody');
    var $thead = $('#table').find('thead');

    $.ajaxSetup({ cache: false });

    $.ajax({
        crossDomain: true,
        type: "GET",
        url: url,
        cache: false
    }).done(function (data) {
        alert("lev 2 ");
        var ObjectKeys = Object.keys(data.query.results.rate[0]);
        var row = "<tr>";
        row += "</tr>";
        $thead.append(row);

        $.each(data.query.results.rate, function (i, el) {
            console.log("lev 3 = " + i);
            $tbody.append($('<tr />').append($('<td />').text(el.id)).append($('<td />').text(el.Name)).append($('<td />').text(el.Rate)).append($('<td />').text(el.Ask)).append($('<td />').text(el.Bid)));
        });
    });        
});

How can I solve this problem?

DEMO Fiddle Here

Upvotes: 2

Views: 727

Answers (1)

Akhilesh Singh
Akhilesh Singh

Reputation: 1724

The issue is IE8 doesn't support the Cross Origin Resource Sharing (CORS) XHR, so you can't do a cross domain ajax call with the native XHR or jQuery's $.ajax.

For IE8, Microsoft decided to come up with their own cross domain XHR instead of using the CORS XHR, which is called XDomainRequest, so you'll have to implement that to support IE8 users. An example usage can be found in this answer.

Alternatively, you could proxy the cross domain request through your local server side, making the external request a server to server situation, which won't be subject to Same Origin Policy.

Upvotes: 1

Related Questions