0Pricing
Flask Academy · レッスン

ブラウザークライアント向けにCORSを設定する

信頼できるオリジンからAPIを呼び出せるようにします

「ブラウザークライアント向けにCORSを設定する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Browsers Block Calls

A browser refuses cross-origin requests by default for safety. This same-origin policy stops one site from quietly reading another.

What CORS Actually Is

CORS is a set of response headers that say which other origins may call your API. It opens the door on purpose.

An Origin Defined

An origin is the scheme, host, and port together. A different port or http versus https counts as a separate origin.

Meet Flask-CORS

You rarely write the headers by hand. The Flask-CORS extension adds the right ones for you on every response.

pip install Flask-CORS

Enable It Globally

The simplest setup wraps the whole app. Calling CORS(app) allows every origin, which is fine while learning.

from flask_cors import CORS
CORS(app)

Restrict to Trusted Origins

In real apps you name who may call you with origins. Anything not on the list is still blocked by the browser.

CORS(app, origins=["https://app.example.com"])

Limit Which Routes

You can scope CORS to a path prefix with resources. Here only your API namespace is opened to outside callers.

CORS(app, resources={r"/api/*": {"origins": "*"}})

The Key Header

Under the hood the magic is Access-Control-Allow-Origin. The browser reads it to decide whether to hand the response to your code.

Preflight Requests

For some calls the browser first sends an OPTIONS preflight to ask permission. Flask-CORS answers it automatically.

Allowing Credentials

To send cookies cross-origin you must set supports_credentials. Note that a wildcard origin is then no longer allowed.

CORS(app, supports_credentials=True)

CORS Is Not Auth

Remember that CORS only guides browsers. It is not a security wall, so you still need real authentication on your API.

Quick Check

Choose where the CORS decision is enforced.

Recap

You saw why browsers block cross-origin calls, added Flask-CORS, restricted origins and routes, met preflight, and learned CORS is not auth. Well done!

よくある質問

「ブラウザークライアント向けにCORSを設定する」レッスンは無料ですか?

はい。「ブラウザークライアント向けにCORSを設定する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「ブラウザークライアント向けにCORSを設定する」で何を学びますか?

信頼できるオリジンからAPIを呼び出せるようにします ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「ブラウザークライアント向けにCORSを設定する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Flask-Limiterでリクエストを制限する
  2. ブラウザークライアント向けにCORSを設定する
  3. セキュリティヘッダーとHTTPS
  4. インジェクションを防ぐため入力を検証する
← Flask Academyに戻る