gpt4 book ai didi

javascript - Pixabay API CORB 问题

转载 作者:行者123 更新时间:2023-12-02 21:41:39 26 4
gpt4 key购买 nike

当我尝试从我的 JavaScript (React) 应用程序中访问 Pixabay 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 错误。

我需要做什么才能取消阻止我的请求?

最佳答案

我猜您正在使用 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);
}
);

关于javascript - Pixabay API CORB 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60352829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com