--- url: "https://web.dev/articles/codelab-same-origin-fetch" titulek: "Same Origin Policy & Fetch requests | Articles | web.dev" h1: "Same Origin Policy & Fetch requests Stay organized with collections Save and categorize content based on your preferences." popis: "In this codelab, learn how the same-origin policy works when fetching resources." jazyk: "en" kanonicka: "https://web.dev/articles/codelab-same-origin-fetch" drobecky: ["Articles", "Same Origin Policy & Fetch requests"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:47:08" znaku: 1964 slov: 291 kodovani: "iso-8859-2" sha256: "dc96062cb5d6b65f5d10689f29177473bc228b8cf87663fffd72d3e2e702ae93" --- Same Origin Policy & Fetch requests Stay organized with collections Save and categorize content based on your preferences. Mariko Kosaka In this codelab, see how the same-origin works when fetching resources. Set up: Fetch page from same origin The demo is hosted at `https://same-origin-policy-fetch.glitch.me`. This simple web page uses `fetch` to load resource from `https://same-origin-policy-fetch.glitch.me/fetch.html`. Since `index.html` and `fetch.html` share the same origin, you should see `200` displayed on the live preview. 1. Fetch page from different origin Try to change the fetch URL to `https://www.google.com`. What do you see in the live preview? The browser should have blocked the fetch request because you requested a resource from a different origin. This means an attacker cannot read cross-origin resources even if they have taken control of a user's browser. 2. Fetch a cross-origin resource Try changing the fetch URL to `https://api.thecatapi.com/v1/images/search`. What do you see in the live preview? The fetch URL is a different origin, but you should see the status code 200. Why? Modern web applications often request cross-origin resources to load third-party scripts or query an API endpoint. To accommodate these use cases, there is a mechanism called CORS (Cross Origin Resource Sharing) to tell the browser that loading of a cross-origin resource is allowed. See [Share cross-origin resources safely](/articles/cross-origin-resource-sharing) for more on CORS. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2018-11-05 UTC.