我想从Instagram的API的一些数据,但我不断收到以下错误: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.instagram.com/v1/users/1652250683/media/recent/?MYTOKEN&count=20&callback=?. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
我也查了Instagram的开发站点的一切,所有的选择似乎罚款。 这是我的代码:取CORS误差与Instagram的API
fetch('https://api.instagram.com/v1/users/1652250683/media/recent/?MYTOKEN&count=20&callback=', {
mode: 'cors'
})
.then(response => response.json())
.then(function(data) {
const response = data;
const images = response['data'].map(img => {
return [img.images.standard_resolution.url]
})
const topImgs = images.slice(0, 7);
const leftImgs = images.slice(7, 10);
const rightImgs = images.slice(10, 13);
const bottomImgs = images.slice(13, 20);
let topDivs = document.querySelector('.top').children;
let leftDivs = document.querySelector('.left').children;
let rightDivs = document.querySelector('.right').children;
let bottomDivs = document.querySelector('.bottom').children;
function* enumerate(iterable) {
let i = 0;
for (const x of iterable) {
yield [i, x];
i++;
}
}
for (const [i, div] of enumerate(topDivs)) {
div.innerHTML = `<img src="${topImgs[i]}">`
}
for (const [i, div] of enumerate(leftDivs)) {
div.innerHTML = `<img src="${leftImgs[i]}">`
}
for (const [i, div] of enumerate(rightDivs)) {
div.innerHTML = `<img src="${rightImgs[i]}">`
}
for (const [i, div] of enumerate(bottomDivs)) {
div.innerHTML = `<img src="${bottomImgs[i]}">`
}
})
.catch(function(error) {
// If there is any error you will catch them here
console.log(error);
});
正如你可以看到,我只想要得到的图像相当straightforward.Locally我使用CORS插件Chrome和它工作正常,但在服务器上没有。有没有人有任何想法可能是什么问题,以及如何解决它?
'&回调='...建议你期望JSONP - 使用提取或XMLHttpRequest的JSONP你没有得到对于这一点......你使用src属性的脚本标记(或者如果你必须的话,使用JQuery) –
我刚刚加了“?”作为最后一次尝试,看看它是否可以像那样工作。原始代码只有“&callback =”我会编辑帖子来强调这个 – Zvezdas1989
这不是?这是回调=表明它是一个JSONP类型的API。这意味着你不能像我说的那样使用fetch或xmlhttprequest。你需要阅读JSONP的工作方式 –