It could be that localhost is not allowed as a domain to do these requests. If you would use a valid domain name it should work correctly
Oct 8, 2020
8
Level 5
Why CORS error with axios request to youtube/v3/video
Hello, In my @vue/cli 4.0.5 / axios": "^0.19 I got “No 'Access-Control-Allow-Origin' header is present on the requested resource.” when I run get request to youtube/v3/video API. I read this https://developers.google.com/youtube/v3/docs/videos docs and do :
let params = { // https://developers.google.com/youtube/v3/docs/search/list
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods' : 'GET, POST, PUT, DELETE, OPTIONS',
part: 'snippet',
key: this.youtubeApiKey,
id : this.videoId,
type: 'video'
}
axios.get('https://www.googleapis.com/youtube/v3/video', params)
.then(response => {
console.log(response)
})
.catch(error => {
console.error(error)
})
and got error in console:
%5Bobject%20Object%5D:1 Access to XMLHttpRequest at 'https://www.googleapis.com/youtube/v3/video' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
where localhost:8080 is my localhost which I got with command :
yarn run serve
In this vuejs project I use axios for reading data from external API with code like :
let apiUrl = process.env.VUE_APP_API_URL
settingCredentialsConfig.headers.Authorization = 'Bearer ' + this.getters.token
axios.get(apiUrl + '/personal/task_assigned_to_users', settingCredentialsConfig)
.then((response) => {
and settingCredentialsConfig is in config file:
export const settingCredentialsConfig = {
withCredentials:true,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials':true
}
}
Seems, I set the same parameters when I make request to youtube, but why error and how to fix it?
Thanks!
Please or to participate in this conversation.