Search code examples
javascriptjqueryjsonxmlmatomo

XML value from URL to text using Javascript


I am trying to get a value from this URL (returns pure XML):

http://demo.piwik.org/?module=API&method=VisitsSummary.getUniqueVisitors&idSite=7&period=day&date=today&format=xml&token_auth=anonymous

And I want to store this value in this element on a separate site:

<div id="result" style="color:red"></div>

Every javascript or jquery attempt I try results in some "access-control-origin" error, which I understand to a point but I can't do anything about the remote server. I need a quick front-end solution.

Note: There is another format I can return the data in - JSON. But I have had similar issues above in trying to get that data as well.


Solution

  • Because you are trying to make cross domain request and because server only allow jsonp for cross domain request, then use jsonp. For example:

    $.ajax({
        url: 'http://demo.piwik.org/?module=API&method=VisitsSummary.getUniqueVisitors&idSite=7&period=day&date=today&format=json&token_auth=anonymous',
        dataType: 'jsonp'
    }).done(function(data){
        $('#result').html(data.value);
    });
    

    -jsFiddle-