Pixabay API CORB问题

问题描述 投票:0回答:1

[尝试从我的Javascript(React)应用程序中访问PID api时出现跨域读取阻止(CORB)。

CORB issue

这是我的提取代码:

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);
    }
  );

获取操作返回的数据很好,但是在简单的img标签中使用的每个单独的图像url都会引发CORB错误。

取消阻止我需要做什么?

javascript api security fetch cross-origin-read-blocking
1个回答
1
投票

[我猜您正在使用到png的网站的链接作为图像的源代码(hit.pageURL)。您不是要使用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);
    }
  );
© www.soinside.com 2019 - 2024. All rights reserved.