0

I am trying to learn how to request JSON data with the JavaScript Fetch-API to fetch data from an open data portal called CKAN.

Following the Google documentation I tried this code:

fetch('https://ckan.open.nrw.de/api/3/action/package_list')
 .then(
  function(response) {
  if (response.status !== 200) {
    console.log('Looks like there was a problem. Status Code: ' +
      response.status);
    return;
  }

  // Examine the text in the response
  response.json().then(function(data) {
    console.log(data);
  });
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});

This function catches an object Error (when opening the URL in my browser, I get a valid JSON):

"Fetch Error :-S", [object Error] { ... }

What is the problem here?

Here is a working jsfiddle.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
feinheitsbrei
  • 51
  • 2
  • 5
  • 2
    If you look in the browser console, you will see it's a CORS error. – Shoejep Feb 09 '22 at 12:51
  • 1
    Browser will block any response from different domains because of Same Origin Policy. If you want to fetch data from this api endpoint, you need to use server-side approach to get the data and it means you need a proxy server to help you to get the data from the browser. – ikhvjs Feb 09 '22 at 12:54

0 Answers0