我已尽我所能使此代码正常工作。我正在使用React。我在下面发布了一个端点。我尝试使用提取然后再使用超级代理(也许很重要),但我都无法正常工作。我得到了这两个错误。仅供参考,我有意“ x”出了userId和访问令牌。OPTIONS https://api.instagram.com/v1/users/xxxxxxxxx/media/recent/?access_token=xxxxxxxx 405 ()
和Failed to load https://api.instagram.com/v1/users/xxxxxxx/media/recent/?access_token=xxxxxx: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405.
我尝试了this post和this post并实现了他们的解决方案,但是一个返回的对象没有我需要的数据,而另一个则不起作用。但是,当我将完整的URL粘贴到浏览器中时,便能够取回JSON对象。请帮忙。
export const getIGPost = () => dispatch => {
// fetch('https://api.instagram.com/v1/users/xxxxxxxx/media/recent/?
access_token=xxxxxxx',{
// method:'GET',
// dataType:'jsonp',
// crossDomain:true
// })
request
.get(`https://api.instagram.com/v1/users/xxxxxxxxx/media/recent/?access_token=xxxxxxxxx`)
.set('Access-Control-Allow-Origin', 'http://localhost:3000')
.then(res => {
if(res.ok) {
console.log(res.body)
// dispatch(addIGPost(res.body))
}
})
}
最佳答案
解决此问题的简单方法是使用CorsAnywhere。
关于javascript - Instagram API获取JavaScript Access-Control-Allow-Origin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47122006/