Read an XML in another domain

3

Is it possible to read an xml in another domain? ex:

http://mfmradio.fr/winradio/prog10.xml?player201507291645

I made the code:

<script type="text/javascript">
        var i;
        var timer,timeout2;
        $(document).ready(function(){
            LoadProg = function(timeout){
                timeout2 = timeout;
                $.ajax({
                    cache: "false",
                    type: "POST",
                                        url: 'http://mfmradio.fr/winradio/prog10.xml?player201507291645',
                                        dataType: 'xml',
                    success: function(xml, textStatus, XMLHttpRequest){
                        $('#title-list').html('');

                        var pochetteEnCours = $(xml).find('morceau:eq(0)').find('pochette').text();
                        var pochetteEnCours = pochetteEnCours.replace(/^\s+|\s+$/g,"");



                        $('<li class="pochette"><\/li>').html(
                            pochette
                        ).appendTo('#title-list');
                        $('<li class="artiste"><\/li>').html(
                            $(xml).find('morceau:eq(0)').find('chanteur').text()
                        ).appendTo('#title-list');
                        $('<li class="titre"><\/li>').html(
                            $(xml).find('morceau:eq(0)').find('chanson').text()
                        ).appendTo('#title-list');

                        var musiqueEnCours = "J'écoute " + $(xml).find('morceau:eq(0)').find('chanteur').text() + " - " + $(xml).find('morceau:eq(0)').find('chanson').text();
                        $('<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?via=MFMRadio&amp;text='+musiqueEnCours+'&amp;count=horizontal"style="width: 110px; hauteur: 20px; position:absolute; margin-top:80px; margin-left:190px;"></iframe>').appendTo('#button_twitter');

                        // Last Titles

                        $('#last-titles-list').html('');
                        $('prog morceau:gt(0):lt(4)', xml).each(function(){

                            var pochetteArchive = $(this).find('pochette').text();
                            var pochetteArchive = pochetteArchive.replace(/^\s+|\s+$/g,"");

                            if( (typeof( pochetteArchive ) != "undefined") && (pochetteArchive != '') ){
                                var pochette = '<img src="' + $(this).find('pochette').text() +'"/>';
                            }else{
                                var pochette = '<img src="/media/pochette.png"/>';
                            }

                            $('<li><\/li>').append(
                                $('<span class="pochette"><\/span>').html(pochette)
                            ).append(
                                $('<span class="artiste"><\/span>').html($(this).find('chanteur').text())
                            ).append(
                                $('<span class="titre"><\/span>').html($(this).find('chanson').text())
                            ).appendTo('#last-titles-list');
                        });

                    }
                });

                timer = setTimeout("LoadProg(timeout2)", timeout);
            }
        });


        $(document).ready(function(){
            LoadProg(20000);
        });


    </script>

But it returns the error:

XMLHttpRequest cannot load http://mfmradio.fr/winradio/prog10.xml?player201507291645. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.doratiotto.com.br' is therefore not allowed access.

As I do not have access to the domain I could not for example create a crossdomain.xml

    
asked by anonymous 29.07.2015 / 17:00

2 answers

1

If the server http://mfmradio.fr using apache and xml is static , you can use .htaccess with mod_headers, for example:

Header add Access-Control-Allow-Origin "*"

This will allow any external domain to access .xml , to define a specific domain change * to a url.

If you do not have access to http://mfmradio.fr files, you will need to create a "proxy" to read this xml, if the server that is ajax is using php, you can use curl , for example:

Create a file named proxy.php :

<?php
if (false === isset($_GET['url'])) {
   echo 'Url não definida';
   exit;
}

$url = $_GET['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYPORT, 80);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

$data = curl_exec($ch);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

header('Content-Type: ' . $contentType);

echo $data;

And Ajax should look like this:

$.ajax({
    cache: "false",
    type: "POST",
    url: 'proxy.php?url=' + encodeUri('http://mfmradio.fr/winradio/prog10.xml?player201507291645'),
    dataType: 'xml',
    
29.07.2015 / 21:26
1

It is possible, as long as the domain owner allows the request:

PHP:

// Permite apenas alguns domínios
header('Access-Control-Allow-Origin: http://mysite1.com');
header('Access-Control-Allow-Origin: http://example.com');
header('Access-Control-Allow-Origin: https://www.mysite2.com');
header('Access-Control-Allow-Origin: http://www.mysite2.com');

// Permite QUALQUER domínio
header('Access-Control-Allow-Origin: *');  // Permite qualquer domínio

HTACCESS

# Permite alguns domínios
SetEnvIf Origin "^http(s)?://(.+\.)?(domain\.org|domain2\.com)$" origin_is=$0 
Header always set Access-Control-Allow-Origin %{origin_is}e env=origin_is

# Permite QUALQUER domínio
Header set Access-Control-Allow-Origin "*"

I believe that Access Control is blocked by default, keeping it enabled on any occasion would be a security breach.

Failure example:

  

Imagine a site called faceboock.com and it was just a blank page that requested Facebook's login page for AJAX and only changed the action of the login form to http://faceboock.com/rouba-conta-facebook.php .

    
29.07.2015 / 21:06