0
votes

I'm writing a currency exchange widget and i can't get how to parse data from this xml: http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

Here's my code:

jQuery('#value_from').on('keyup', function() {
    var data = this.value;
    var curr = document.getElementById('curr_from').value;
    jQuery.ajax({
        type: "GET",
        url: "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml",
        dataType: "xml",
        success: function(xml) {
            jQuery(xml).find('Cube').each(function(){
                var data = jQuery(this).attr('currency');
                console.log(data);
            });
        }
    });
    document.getElementById('value_to').value = '\u20AC ' + data;
});
1
jQuery should parse the XML automatically. Doesn't it? What does console.log(xml) reveal?Álvaro González
@ÁlvaroG.Vicario it says there's an xml parsing errorMariano
That's the exact complete output of console.log(xml)?Álvaro González
Where are you executing this code, if it is from a different domain it won't work without jsonp. Same-origin policyDanny
@Mariano php isnt executed in the browser hence why you can make calls to other domains. Javascript is executed on the user's machine and doesn't allow it for security reasons. You can write php to fetch that xml and then in the jQuery call your php that is hosted on the same domain.Danny

1 Answers

1
votes

change this:

jQuery(xml).find('cube')

to this:

jQuery(xml).find('Cube')
    //------------^-------uppercase 'C'

In your xml file i just seen that you have the xml node as Cube but you are finding it with lowercase c as cube.


Note:

As this is a cross domain data access hence dataType:"xml" won't work here, as per docs cross domain data can be accessed only with dataType:"jsonp".