5

As you can see here https://mohamed-hassan.com/samples/project24/ , if you open the console you will see this error

(ZYLR4KocS2Q?enablejsapi=1&autoplay=1&mute=1&controls=0&showinfo=0&modestbranding=1&rel=0:1 Failed to load https://googleads.g.doubleclick.net/pagead/id: Redirect from 'https://googleads.g.doubleclick.net/pagead/id' to 'data:text;charset=utf-8,' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.youtube.com' is therefore not allowed access.)

I just embedded video from youtube using iframe tag and I added at the top of the index file header('Access-Control-Allow-Origin: *'); but still get this error.

Here is a screenshot enter image description here

vahdet
  • 6,357
  • 9
  • 51
  • 106
Mohamed Hassan
  • 129
  • 1
  • 1
  • 9
  • See this [SO post](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) for further info about your error. Also you can try to check [this post](https://ourcodeworld.com/articles/read/291/how-to-solve-the-client-side-access-control-allow-origin-request-error-with-your-own-symfony-3-api) for the step by step solution.For YouTube API reference, open [this link](https://developers.google.com/youtube/v3/). – MαπμQμαπkγVπ.0 Jul 24 '18 at 11:40

2 Answers2

8

You forgot to add &origin=https://yourdomain.com to your iframe src, this value allows YouTube to add the proper headers to its response.

  • how to write that code with iframe ? sorry im new in programming tho. – Febri Tahta Nov 11 '20 at 09:21
  • 1
    Fixed for me! @FebriTahta so for example, if your youtube link is `https://www.youtube.com/watch?v=3AXaLzjv3O0` and your website is `https://example.com`, for the youtube link of your video, put this entire thing: `https://www.youtube.com/watch?v=3AXaLzjv3O0&origin=https://example.com` – user3035649 Apr 14 '22 at 16:33
0

Error fix with this code:

<iframe class="lazy" allowfullscreen="1" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" src="https://www.youtube.com/embed/youtube_video_id?origin=https://yourdomain.com&showinfo=0&video-id=youtube_video_id&enablejsapi=1&widgetid=1&color=white&modestbranding=1&rel=0" data-title="video_title" title="video_title" frameborder="0"></iframe>
  • Note: setting the `color` parameter to white will disable the `modestbranding` option. : https://developers.google.com/youtube/player_parameters – Marcio Duarte May 20 '22 at 17:52