CORS error with window.fetch on YouTube resource

时光总嘲笑我的痴心妄想 提交于 2019-12-02 02:29:10

问题


I'm using window.fetch to retrieve a YouTube resource with the following URL:

http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=[id]&format=json

Using Fiddler, I see that I am getting the expected reply from YouTube's servers containing JSON data. However, I'm not able to use it as I'm getting a Javascript error.

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:60366' is therefore not allowed access.

I tried enabling CORS on my window.fetch but I read that this will only work if the server allows CORS in its header response. YouTube doesn't have this header field in its reply.

How do I enable my script to accept a response from YouTube using window.fetch? (I'm using Chrome v44.)

window.fetch(
            url, {
                method: 'GET',
                mode: 'cors',
                headers: {
                    'Accept': 'text/plain',
                    'Content-Type': 'text/plain'
                }
            })
...

回答1:


I think you need to use youtube API to be able to make CORS request.

https://developers.google.com/youtube/v3/



来源:https://stackoverflow.com/questions/32291557/cors-error-with-window-fetch-on-youtube-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!