【问题标题】:Pixabay API CORB issueAPI CORB 问题
【发布时间】:2020-06-06 17:17:16
【问题描述】:

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

这是我的获取代码:

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 错误。

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

【问题讨论】:

  • 基于我刚刚运行的测试,以及您发布的屏幕截图,我可以判断 API 请求没有问题(它返回 20 次点击,正如您在屏幕截图中看到的那样,正好有 20 个 CORB 问题,因此您成功解析了结果)。问题可能是您之后如何处理这些结果。请贴出相关代码。并点击20旁边的小箭头,看看哪些网址是问题的根源
  • 感谢您的快速回复 blex :) 我已经更新了问题,希望信息足够
  • 似乎我使用的任何 pixabay url,甚至硬编码到 img 标签中都会显示此错误..
  • API 是否设置了后续请求可能需要的 cookie?在文档中找不到太多关于此的内容

标签: javascript api security fetch cross-origin-read-blocking


【解决方案1】:

我猜你正在使用指向 Pixabay 网站的链接作为图片的来源 (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);
    }
  );

【讨论】:

  • 哇!谢谢大佬,好地方我今天刚学到一些东西 :) 我知道每个图像的所有选项,但不认为每个字段会有不同的安全级别,现在一切都有意义..
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2019-06-21
  • 2020-09-30
  • 2019-12-12
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多