javascriptsecurityfetch-apicross-origin-read-blocking

Pixabay API CORB issue


I am getting Cross-Origin Read-Blocking (CORB) when trying to access the Pixabay API from within my JavaScript (React) app.

CORB issue

Here is my fetch code:

fetch(`https://pixabay.com/api/?key=${API_KEY}&q=travel&image_type=photo&pretty=true`)
  .then(res => res.json())
  .then(
    result => {
      console.log(result);
      // set url array in state to be used by a gallery component..
    },
    error => {
      console.log(error);
    }
  );

The fetch returns the data fine but the each of the individual image urls used in a simple img tag throws a CORB error.

What do I need to do to unblock my request?


Solution

  • I guess you're using the links to Pixabay's website as src for your images (hit.pageURL). Didn't you mean to use hit.previewURL?

    const API_KEY = '123456789abcdef';
    
    fetch(`https://pixabay.com/api/?key=${API_KEY}&q=travel&image_type=photo&pretty=true`)
      .then(res => res.json())
      .then(
        result => {
          // Just for the demo
          const html = result.hits.map(
            hit => `<a href="${hit.pageURL}"><img src="${hit.previewURL}"></a>`
          ).join('');
          document.body.innerHTML = html;
        },
        error => {
          console.log(error);
        }
      );