1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CORSで怒られたら

Last updated at Posted at 2021-11-11

当該エラー

Access to XMLHttpRequest at 'https://server-hoge' from origin 'https://client-hoge' 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.

サーバー側(FastAPIの場合)

公式ドキュメントなどを元にCORSの許可を設定

app = FastAPI()

origins = [
    "https://client-hoge",
    "http://localhost",
    "http://localhost:8080",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

クライアント側(Flutterの場合)

'Access-Control-Allow-Origin': '*'をヘッダに追加

headers: <String, String>{
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*'
    }

CORSについて

CORSについてはこの方が詳しく書いているのでそちらを是非
https://qiita.com/att55/items/2154a8aad8bf1409db2b

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?