Andrei RRR
Andrei RRR

Reputation: 3162

AJAX External URL?

I am developing a simple Mobile App that displays currency rates from an XML file. Now, the problem is that I read the XML from my server so I have to put the full URL in .ajax but it dosen't seem to work with external URLs. Can someone tell me how can I fix this or how to replace the code so it will work?

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "http://www.domain.com/currencies.xml",
    dataType: "xml",
    success: function(xml) {



    $(xml).find('Currency').each(function(){

    var Name = $(this).find('Name').text();
    var ValueUSD = $(this).find('ValueUSD').text();
    var ValueEUR = $(this).find('ValueEUR').text();
    var ExValueUSD = $(this).find('ExValueUSD').text();
    var ExValueEUR = $(this).find('ExValueEUR').text();

    $('#content').append('<div class="currencyBox"><div class="currency">'+Name+'</div><div class="tab1"><div class="half">'+ValueUSD+'</div><div class="half">'+ValueEUR+'</div></div><div class="tab2"><div class="half">'+ExValueUSD+'</div><div class="half">'+ExValueEUR+'</div></div></div>');

    });



    }
  });
});

</script> 

Upvotes: 1

Views: 567

Answers (2)

Drachenfels
Drachenfels

Reputation: 3286

Look at jquery doc about jsonp. There is possibility to do cross site request in html5 especially, however most tools (firebug, noscript, adblock) and some browsers on default are bloking such requests. So I would stick to jsonp as most reliable source.

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

Ajax Doesnot work with Cross-Domain due to same origin policy. Try JSONP in such case.

Upvotes: 0

Related Questions