[尝试从我的Javascript(React)应用程序中访问PID 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错误。
取消阻止我需要做什么?
[我猜您正在使用到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);
}
);