0

I need to retrieve data from an api but the data is only available in XML. I was told to retrieve the data to display it in HTML via this topic: How to retrieve values from xml file and display in html webpage?

Until I thought I was right, I changed the code with the info I needed and got the famous error in the Google Chrome console: (index):30 Access to XMLHttpRequest at 'xxx' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

So in my index.html file I added this PHP code at the beginning : <?php header('Access-Control-Allow-Origin : *'); ?> (I use the * for the tests only, to publish my code I will set the exact URL).

My page still shows this error, so I'm wondering if it's really on my code that I should put this, or on the recipient's server?

Moerip
  • 61
  • 8
  • 2
    I'm a bit confused on the interaction you're describing. You're trying to enable CORS in your own server-side application? What is "the recipient's server"? CORS errors happen when a page running in the browser tries to make an AJAX request to a 3rd party resource. Is that what's happening here? Or is the page trying to access *your own* server? Or something else? – David Mar 18 '22 at 12:47
  • @David Thanks for your feedback. To make it simple, the XML file that I am trying to retrieve the data from is not on my server. It is a link to an external server, and we have access to a list of data in XML format. On our side, we have to format this data, so I tried to retrieve the info via what I put as a link above. – Moerip Mar 18 '22 at 12:52
  • 2
    It is not up to YOU to set the access headers. It is up to the TARGET. If they are not set, you are SOL unless you use your php/server as a proxy for the api on the other server – mplungjan Mar 18 '22 at 12:54
  • 3
    If the 3rd party server doesn't allow CORS requests then there's nothing you can do to change that from your end. What you *can* do is request the resource from your server-side code and deliver the results to the client, essentially using your own server as a proxy for the request. – David Mar 18 '22 at 12:54
  • What does "_we have access to a list of data in XML format_" exactly means? – Teemu Mar 18 '22 at 12:58

0 Answers0