问题
componentDidMount() {
let token = this.getAccessToken('access_token');
let config = {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
Accept: 'application/json'
}
}
if (token) {
axios.get('https://api.instagram.com/v1/users/self/?access_token=' + token, config)
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
}
Trying to make an call to Instagram's API but I keep getting this error:
I'm using React.
Any help would be great.
回答1:
I don't think Instagram supports cross origin requests. They do support jsonp though.
Access-Control-Allow-Origin with instagram api
instagram jquery ajax type="GET, can't get around CORS
回答2:
You can use a middleman to make your request server-side. There are several to choose from when I search 'no cors npm', but here is an example: https://www.npmjs.com/package/cors
来源:https://stackoverflow.com/questions/46615606/how-to-make-api-call-to-instagram-with-axios-without-cors-error