jQuery xml error ' No 'Access-Control-Allow-Origin' header is present on the requested resource.'

后端 未结 2 1271
终归单人心
终归单人心 2020-11-22 09:48

I am working on this personal project of mine just for fun where I want to read an xml file which is located at http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

2条回答
  •  半阙折子戏
    2020-11-22 10:24

    There's a kind of hack-tastic way to do it if you have php enabled on your server. Change this line:

    url:   'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml',
    

    to this line:

    url: '/path/to/phpscript.php',
    

    and then in the php script (if you have permission to use the file_get_contents() function):

    
    

    Php doesn't seem to mind if that url is from a different origin. Like I said, this is a hacky answer, and I'm sure there's something wrong with it, but it works for me.

    Edit: If you want to cache the result in php, here's the php file you would use:

     time() + $ageInSeconds) {
      $contents = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
      file_put_contents($cacheName, $contents);
    }
    
    $xml = simplexml_load_file($cacheName);
    
    header('Content-type: application/xml');
    echo $xml;
    
    ?>
    

    Caching code take from here.

提交回复
热议问题