首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pixabay API CORB问题

Pixabay API CORB问题
EN

Stack Overflow用户
提问于 2020-02-22 21:54:46
回答 1查看 596关注 0票数 0

当我试图从我的Javascript (React)应用程序中访问Pixabay api时,我得到了跨域读取阻止(CORB)。

下面是我的抓取代码:

代码语言:javascript
复制
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);
    }
  );

fetch可以很好地返回数据,但是在一个简单的img标记中使用的每个单独的图像urls都会抛出一个CORB错误。

我需要做什么来解除对我的请求的阻止?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-22 22:34:50

我猜你正在使用Pixabay网站的链接作为你的图片的src (hit.pageURL)。你不是想用hit.previewURL吗?

代码语言:javascript
复制
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);
    }
  );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60352829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档